Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 265
prograhammer Avatar asked Mar 11 '15 00:03

prograhammer


People also ask

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

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.


2 Answers

`

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!

like image 129
GeraldBiggs Avatar answered Nov 15 '22 06:11

GeraldBiggs


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!

like image 24
Carter Avatar answered Nov 15 '22 08:11

Carter