6
votes

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.

4
I don’t know about you, but I tend to favour naming my fields after the data they accept. If it’s a username, I’ll label it as such. If it’s an email address, same deal.Martin Bean
@MartinBean that's a simple example. But after you've done all the work and a stakeholder says "That label won't make sense to our end-users" then you are in trouble.prograhammer
$ php artisan make:migration rename_username_to_emailMartin Bean
I wouldn't say that's how you respond to a superficial demand.prograhammer
If they have a valid point. If users authenticate via email address, then I’d name the field email and not username.Martin Bean

4 Answers

8
votes

`

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!

4
votes

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!

2
votes

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.

1
votes

Use this:

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