How to give custom field name in laravel form validation error message

前端 未结 6 1141
没有蜡笔的小新
没有蜡笔的小新 2021-01-07 20:21

I was trying form validation in laravel. I have a input text field in my form called \'Category\' and i\'m given the field name as \'cat\' as short.

And i defined

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-07 20:42

    You can specify custom error message for your field as follows.

    $messages = array(
        'cat.required' => 'The category field is required.',
    );
    
    $validator = Validator::make($input, $rules, $messages);
    

    Please see Custom Error Messages section in laravel documentation for more information.

    Or you can keep a mapping for your field names like below. And you can set those into you validator. So you can see descriptive name instead of real field name.

    $attributeNames = array(
       'name' => 'Name',
       'cat' => 'Category',     
    );
    
    $validator = Validator::make ( Input::all (), $rules );
    $validator->setAttributeNames($attributeNames);
    

提交回复
热议问题