Is there a way to alias input names in the rules for Laravel 5 validation?

10,410

Solution 1

`

use App\Http\Requests\Request;

class AuthLoginRequest extends Request {

public function rules()
{
    return [
        'username' => 'required|email',
        'password' => 'required'
    ];
}

public function messages()
{
    return [
        'username.required' => ':attribute is required',
        'username.email' => ':attribute is invalid'
    ];
}

public function attributes()
{
    return[
        'username' => 'email', //This will replace any instance of 'username' in validation messages with 'email'
        //'anyinput' => 'Nice Name',
    ];

}

}`

Updated

I believe anywhere there's an :attribute in your messages will be either default to the name set in your form, or overridden by the method attributes() above. (on :attribute http://laravel.com/docs/5.0/validation#custom-error-messages)

Footnote: I have not explained how I'm doing a dynamic array for my form, as I'm iterating over each form input in a way which may be clumsy. I did notice that I'm using the following line for my form request. I thought I'd mention that here in case:

use Illuminate\Foundation\Http\FormRequest;

Let me know if this is more helpful!

Solution 2

You can customise your validation input names in file resources/lang/en/validation.php, assuming you are using Laravel 5 and using English as locale language by default.

You can find an array called 'custom', simply customise your input names and validation messages there.

For example:

'custom' => array(
    'username' => array(
        'email' => 'Username is actually an email.'
    ),
)

where 'username' is your input name, 'email' is the name of built-in rule, and 'Username is actually an email' is whatever you want to tell your user.

Hope this helps!

Solution 3

Open the file resources/lang/en/validation.php, where en is the default language of the app. thars if you are using English. At the bottom of the file, update the attributes array as the following:

'attributes' => array( 'password' => 'Password', 'email' => 'Email Address', ),

Add other attribute names and the corresponding error label. It will do the trick you wanted.

Solution 4

Use this:

$validator = Validator::make($data, [
    'username' => 'required|string', // Your field validation
])
->setAttributeNames(
    ['username' => '"Username"'], // Your field name and alias
);
Share:
10,410
prograhammer
Author by

prograhammer

David Graham Developing web applications for The Home Depot.

Updated on June 11, 2022

Comments

  • prograhammer
    prograhammer about 2 years

    I like the feature in Laravel 5 that let's me throw my validation logic into one of Laravel 5's Form Requests and then Laravel will automatically validate it just before my controller's method is ran. However, one thing that is missing is an easy way to "alias" an input name.

    For example (for simplicity sake), say I have a login form with an input field called "username" but it actually accepts an email address. The label on my form says Email. If there's an error, the error will say something like "Username is required". This is confusing since I'm labeling the field as Email on my form.

    What's a good solution for aliasing inputs when using Laravel's validator?

    So far I've come up with two ideas:

    Solution 1: Use Laravel's custom error messages for each rule

    <?php namespace App\Http\Requests;
    
    use App\Http\Requests\Request;
    
    class AuthLoginRequest extends Request {
    
        public function rules()
        {
            return [
                'username' => 'required|email',
                'password' => 'required'
            ];
        }
    
        // This is redundant. And may have other pitfalls?
        public function messages()
        {
            return [
                'username.required' => 'email is required.',
                'username.email' => 'email is invalid.'
            ];
        }
    
        ...
    }
    

    Solution 2: Use my own custom form class to handle changing the input names to their label/alias (username becomes email for validation purposes), running validation, then changing them back.

  • prograhammer
    prograhammer over 9 years
    +1 This is a great solution for when I know globally that an input will be aliased throughout the whole app! But it won't work for a diverse range of forms where I use all kinds of labels that differ from my input names.
  • Carter
    Carter over 9 years
    Is it possible to keep labels consistent with input names?
  • prograhammer
    prograhammer over 9 years
    Can you give a more complete example (doesn't have to be a large form, but use the example I gave in my question if you want)? You've commented out 2 sections and your example still leaves me the duty of having to go into the laravel codebase just to understand it. I think your solution is probably the correct solution here, just need to see it in completion. Thanks!
  • GeraldBiggs
    GeraldBiggs over 9 years
    @DavidGraham Expanded to include your example, hopefully this helps you. It was a late night for me!
  • Pathros
    Pathros over 5 years
    A note for myself: The documentation about it is here. :attribute,:max,:min,:size,:values