laravel - Using form request specific custom validation attributes -


using laravel's localization (http://laravel.com/docs/5.1/localization) have created custom validation attributes provide friendlier validation errors (for instance, 'first name' instead of first name etc).

i using form requests (http://laravel.com/docs/5.1/validation#form-request-validation) in order validate user submissions , there scenarios provide store-specific custom validation attributes (for instance, may have 'name' field brand name in 1 context, , product name in another).

the messages() method allows me specify validation rule specific message overrides, isn't ideal it's not validation message such need override, attribute name (for example, if have 5 validation rules 'email', have provide 5 overrides here, rather 1 override for, let's say, customer email).

is there solution this? note references formatvalidationerrors() , formaterrors() in laravel documentation, there not information on how correctly override these, , i've not had luck in trying.

you can override attribute names, defaulting whatever field name is.

with form request

in form request class override attributes() method:

public function attributes() {     return [         'this_is_my_field' => 'custom field'     ]; } 

with controller or custom validation

you can use 4th argument override field names:

$this->validate($request, $rules, $messages, $customattributes); 

or

validator::make($data, $rules, $messages, $customattributes); 

simple working example

route::get('/', function () {     // data validate     $data = [         'this_is_my_field' => null     ];      // rules validator     $rules = [         'this_is_my_field' => 'required',     ];      // custom error messages     $messages = [         'required' => 'the message :attribute overwritten'     ];      // custom field names     $customattributes = [         'this_is_my_field' => 'custom field'     ];      $validator = validator::make($data, $rules, $messages, $customattributes);      if ($validator->fails()) {         dd($validator->messages());     }      dd('validation passed!'); }); 

Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -