I like the function in Laravel 5 that allowed me to drop my validation logic into one of the requests of the Laravel 5 form, and then Laravel will automatically validate it immediately before running my controller method. However, one thing that is missing is an easy way to “alias” the input name.
For example (for simplicity), let's say I have a login form with an input field called "username", but it actually accepts an email address. An email is written on a label in my form. If there is an error, the error will say something like "Username". This is confusing since I am marked as an email field in my form.
What is a good alias entry solution when using the Laravel validator?
So far I have come up with two ideas:
Solution 1. Use custom Laravel 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'
];
}
public function messages()
{
return [
'username.required' => 'email is required.',
'username.email' => 'email is invalid.'
];
}
...
}
Solution 2. Use your own form class to handle changing the input names in their label / alias (the username becomes email for verification purposes), validation is performed, and then they are changed.
source
share