Fidelis E Peter
Fidelis E Peter

Reputation: 31

Custom Validation Message in Laravel with custom attribute

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?

Upvotes: 3

Views: 2225

Answers (1)

silver
silver

Reputation: 5331

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;
}

Upvotes: 3

Related Questions