3

I am creating a rule for my validation on my laravel app.

i wrote my rules below

public function rules()
    {
        return [
            'title' => 'required|string',
            'taxonomies' => 'required|array',
            'taxonomies.*.id' => 'required|string|exists:taxonomies,id', 
            'taxonomies.*.values' => 'required|array|min:2',
            'taxonomies.*.values.*' => 'required',
            'currency' => 'required|string',
            'client_budget' => 'required|numeric',
        ];
    }

in 'taxonomies.*.values.*' i will be expecting an array which every key on array['values'] should not be empty eg. this is what i expect

[
            'taxonomies' => [
                '0' => [
                    'id' => 7,
                    'values' => [
                        'key1' => 'own value',
                        'key2' => 'own value',
                        'key3' => 'own value'
                    ],
                ],
                '1' => [
                    'id' => 12,
                    'values' => [
                        'key1' => 'own value',
                        'key2' => 'own value',
                        'key3' => 'own value'
                    ],
                ]
            ]
        ];

so if by any change a key is missing it should show an error this is working fine but When showing error message for 'taxonomies..values.', is display as

"The taxonomies.0.values.key1 field is required."

i want it to show something link this

The key1 value is required at array 0

How will i achieve this?

2
  • so there is no error with validation rules but you want to change the message only. Right? Commented Jan 21, 2023 at 5:05
  • 1
    you will need to iterate the taxonomies and write a custom message for each required message. you can create a generic function to make the error message. Commented Jan 21, 2023 at 5:25

1 Answer 1

3

You can passed in a 2nd and 3rd params during validation for message and attribute value

e.i.

$request->validate(
    [
        'taxonomies.*.values.*' => 'required'
    ],
    [
        'taxonomies.*.values.*' => 'The :attribute value is required at array :index'
    ],
    [
        'taxonomies.0.values.key1' => 'key1'
    ]
);

and you'll get error like this

The key1 value is required at array 0

However, since you doing dynamic array, I can only think of manipulating the attributes array based on your request content.

Something like this

public function store( Request $request  ) {
        
    $attributes = [];
    
    foreach ($request->input('taxonomies') as $index => $values) {
        $valValues = $values['values'];
        foreach ($valValues as $key => $value) {
            $attributes['taxonomies.'.$index.'.values.'.$key] = $key;
        }
    }
    
    $request->validate(
        [
            'taxonomies' => 'required|array',
            'taxonomies.*.id' => 'required', 
            'taxonomies.*.values' => 'required|array|min:2',
            'taxonomies.*.values.*' => 'required'
        ],
        [
            'taxonomies.*.values.*' => 'The :attribute value is required at array :index'
        ],
        $attributes,
        
    );

    //valid request, continue;
}

or if you are using Form Request Validation, you can add messages and attributes method in your request validation class.

e.i.

// Your validation rules
public function rules() {...}

public function messages() {
    return [
        'taxonomies.*.values.*' => 'The :attribute value is required at array :index'
    ];
}

//This should cover all attributes taxonomies.*.values.* present in your request and replaces it with the last key
public function attributes() {
    $attributes = [];

    if ( $this->taxonomies && is_array( $this->taxonomies ) )  {

        foreach ($this->taxonomies as $index => $values) {
            $valValues = $values['values'] ?? null;

            if( !$valValues || !is_array( $valValues ) )
                continue;

            foreach ($valValues as $key => $value) {
                $attributes['taxonomies.'.$index.'.values.'.$key] = $key;
            }
        }
    }
    
    return $attributes;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.