Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using form request specific custom validation attributes

Using Laravel's localization (http://laravel.com/docs/5.1/localization) I have created some custom validation attributes to provide friendlier validation errors (for instance, 'First Name' instead of first name etc).

I am using form requests (http://laravel.com/docs/5.1/validation#form-request-validation) in order to validate user submissions and there are scenarios where I would like to provide store-specific custom validation attributes (for instance, I may have a 'name' field that is Brand Name in one context, and Product Name in another).

The messages() method allows me to specify validation rule specific message overrides, but that isn't ideal as it's not the validation message as such we need to override, just the attribute name (for example, if we have 5 validation rules for 'email', we have to provide 5 overrides here, rather than one override for, let's say, Customer Email).

Is there a solution to this? I note references to formatValidationErrors() and formatErrors() in the Laravel documentation, but there is not really any information on how to correctly override these, and I've not had much luck in trying.

like image 273
BrynJ Avatar asked Jul 07 '15 10:07

BrynJ


People also ask

How do I create a custom validation attribute?

To create a custom validation attributeUnder Add New Item, click Class. In the Name box, enter the name of the custom validation attribute class. You can use any name that is not already being used. For example, you can enter the name CustomAttribute.

What is the method used to configure validation rules in form request?

Laravel Form Request class comes with two default methods auth() and rules() . You can perform any authorization logic in auth() method whether the current user is allowed to request or not. And in rules() method you can write all your validation rule.

What is the method used for specifying custom messages for validator errors in form request?

After checking if the request failed to pass validation, you may use the withErrors method to flash the error messages to the session. When using this method, the $errors variable will automatically be shared with your views after redirection, allowing you to easily display them back to the user.

How do I use CustomValidationAttribute?

You apply the CustomValidationAttribute attribute to an entity or one of its members when you need to specify a method to use for validating the entity or the value of the member. Using the first overload is the same as defining custom validation logic for an individual value.


2 Answers

You can override the attribute names, which is defaulting to whatever the field name is.

With form request

In your form request class override the attributes() method:

public function attributes()
{
    return [
        'this_is_my_field' => 'Custom Field'
    ];
}

With controller or custom validation

You can use the 4th argument to override the field names:

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

or

Validator::make($data, $rules, $messages, $customAttributes);

Simple working example

Route::get('/', function () {
    // The data to validate
    $data = [
        'this_is_my_field' => null
    ];

    // Rules for the validator
    $rules = [
        'this_is_my_field' => 'required',
    ];

    // Custom error messages
    $messages = [
        'required' => 'The message for :attribute is 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!');
});
like image 193
user2479930 Avatar answered Oct 27 '22 14:10

user2479930


As detailed in my question, I was looking for a way to provide specific form request stores (http://laravel.com/docs/5.1/validation#form-request-validation) with custom attribute names.

The Laravel documentation only covers two methods for Requests in this context - rules() and authorize(). I was aware there is a messages() method to provide validation specific custom error messages, but it also appears there is an attributes() method, which fits my requirements exactly:

public function attributes()
{
     return [
         'name' => 'Product Name'
     ]
}

This overrides the attribute name in the context of my store.

like image 43
BrynJ Avatar answered Oct 27 '22 15:10

BrynJ