Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.5 FormRequest custom error messages validation not working

I'm using the following standard code to validate a form which makes use of various arrays. The rules are correctly implemented, but when using the standard 'messages()' method override, Laravel does not pick up the messages, but simply returns the standard built in messages.

Output

array (size=7)
  0 => string 'The newitems.0.pickup_date field is required.' (length=45)
  1 => string 'The newitems.0.m_f field is required when newitems.0.item type id is present.' (length=77)
  2 => string 'The newitems.1.pickup_date is not a valid date.' (length=47)
  3 => string 'The newitems.0.size field is required when newitems.0.item type id is present.' (length=78)
  4 => string 'The newitems.1.size field is required when items.1.size unknown is not present.' (length=79)
  5 => string 'The newitems.0.width field is required when newitems.0.item type id is present.' (length=79)
  6 => string 'The newitems.1.width field is required when items.1.width unknown is not present.' (length=81)

FormRequest

UpdateOrderItemsRequest extends FormRequest{

[...]
    public function rules()
    {

        return [
                'items.*.item_type_id' => 'required_with:items.*.m_f,items.*.pickup_date|numeric',
                'items.*.m_f' => 'required_with:items.*.item_type_id',
                'items.*.pickup_date' => 'required_with:items.*.item_type_id|date|nullable',
                'items.*.size' => 'required_without:items.*.size_unknown|numeric',
                'items.*.width' => 'required_without:items.*.width_unknown|numeric',
                'newitems.*.m_f' => 'required_with:newitems.*.item_type_id',
                'newitems.*.pickup_date' => 'required_with:newitems.*.item_type_id|date',
                'newitems.*.size' => 'required_with:newitems.*.item_type_id|required_without:items.*.size_unknown|numeric',
                'newitems.*.width' => 'required_with:newitems.*.item_type_id|required_without:items.*.width_unknown|numeric'
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {

        return [
            'items.*.item_type_id' => 'An item type is required',
            'items.*.m_f' => 'Gender is required',
            'items.*.pickup_date' => 'A pickup date is required',
            'items.*.size' => 'A size is required',
            'items.*.width' => 'A width is required',
            'newitems.*.m_f' => 'Gender is required',
            'newitems.*.pickup_date' => 'A pickup date is required',
            'newitems.*.size' => 'A size is required',
            'newitems.*.width' => 'A width is required'
        ];
    }
}

Is there something dumb and obvious that I'm missing?

like image 563
fred2 Avatar asked Jan 29 '23 10:01

fred2


1 Answers

The custom validation message attribute must end with validation rule name. In your case you should use messages function like as follow

public function messages()
{
    return [
        'items.*.item_type_id.required_with' => 'An item type is required',
        'items.*.m_f.required_with' => 'Gender is required',
        'items.*.pickup_date.required_with' => 'A pickup date is required',
        'items.*.size.required_without' => 'A size is required',
        'items.*.width.required_without' => 'A width is required',
        'newitems.*.m_f.required_with' => 'Gender is required',
        'newitems.*.pickup_date.required_with' => 'A pickup date is required',
        'newitems.*.size.required_with' => 'A size is required',
        'newitems.*.width.required_with' => 'A width is required'
    ];
}
like image 88
Praveen Tamil Avatar answered Feb 02 '23 09:02

Praveen Tamil