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

laravel validation
laravel form validation before submit
name field validation in laravel
image validation laravel
laravel validation array input
laravel 5.8 form validation
laravel validation if exists
laravel validation unique except

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.

`

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!

New Email Validation Rule Options in Laravel 5.8.33, How do I check if an email is valid in laravel? Laravel provides a variety of helpful validation rules; however, you may wish to specify some of your own. One method of registering custom validation rules is using rule objects. To generate a new rule object, you may use the make:rule Artisan command.

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!

Validation - Laravel, php Is there a way to alias input names in the rules for Laravel 5 validation? '​custom' => array( 'username' => array( 'email' => 'Username is actually an email. In bellow seven rules i used in my example but there are several other pre-define rules in Laravel 5 and we can use it. So, in this example i give you one small example from scratch so you can simply understand how to set validation rules in Laravel blade template even if you are beginner.

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.

php Is there a way to alias input names in the rules for Laravel 5 , It makes sense, but what if I was using a multi language application, or as in But for a Laravel-way of developing that kind of looks like too much work. to the validation rules giving the field (the :attribute) a custom name, like this: By default the input filed that comes with Laravel has a name of 'name' Laravel is PHP framework for web artisan. Which helps us to build robust application and APIs. As many of you already know there are many ways to validate request in Laravel.

Use this:

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

Pass a field name to use in Validation messages, I want to modify the input value of a form before it is passed to the validator using the new Request validation in Laravel 5. Sanitize before rules() */ protected function sanitizeInput() { // Autofill alias (aka slug) if submitted empty I think that's the best way to handle input manipulation prior to validation. Attribute names. Laravel has an option to alias input names with custom attributes, as a way to build better error messages. Rule provides an easy way to create them.

[Laravel 5] Modify input before validation, For instance, a registration form probably requires the password to be confirmed. Maybe the Of course, default error messages have been setup for all validation rules. Need to verify uniqueness on a column name other than the attribute name? However, an errors variable ($errors) will still be available in the view. The most concise screencasts for the working developer, updated daily. There's no shortage of content at Laracasts. In fact, you could watch nonstop for days upon days, and still not see everything!

Validation, Decouple validation from Laravel controllers with ValidatorAssistant. You won't miss any feature and since 5.2, it also supports input arrays. Unfortunately, the Laravel Validator doesn't support sub rules for the time names for fields, as an easy way to alias inputs and generate helpful error messages. How to create captcha code in Laravel 5? Today, i want to share with you how to implement simple captcha code with refresh button in laravel 5 app form. In this tutorial you can simply use captcha with validation in your register form or login form as you want. i use mews library package for captcha in laravel 5.5 application, it will help to

fadion/validator-assistant, If we can reuse our Laravel Validation Rules, Messages via json to the rules, angular-validation's messages seems to the input name. when I us between:4, it can be custom by coders, if we can do some alias about messages? time on doing mapping table and some ways to accept outside JSON  input validation service for Laravel 4. Contribute to keevitaja/rulez-laravel development by creating an account on GitHub.

Comments
  • 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.
  • @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.
  • $ php artisan make:migration rename_username_to_email
  • I wouldn't say that's how you respond to a superficial demand.
  • If they have a valid point. If users authenticate via email address, then I’d name the field email and not username.
  • 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!
  • @DavidGraham Expanded to include your example, hopefully this helps you. It was a late night for me!
  • A note for myself: The documentation about it is here. :attribute,:max,:min,:size,:values
  • +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.
  • Is it possible to keep labels consistent with input names?