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.
$ php artisan make:migration rename_username_to_email
– Martin Beanemail
and notusername
. – Martin Bean