Pida
Pida

Reputation: 988

Laravel 5.7: Custom attributes for form validation not working

I'd like to validate a form using both custom messages and attributes. Instead of name: The name may not be greater than 20 characters. the user should see Name: Please use fewer characters., for example.

I'm using AJAX and both keys and values of the response.data.errors object that Laravel returns. Im using Laravel 5.7.

This is a simplified version of the validator function in my RegisterController.

protected function validator(array $data)
{
    // Nice attribute names
    $attributes = [
        'name' => 'Name',
        // ...
    ];

    // Custom messages
    $messages =  [
        'max' => 'Please use fewer characters.'
        // ...
    ];

    // Rules
    $rules = [
        'name'=> 'required|max:20',
        // ...
    ];

    // Working for messages, but not for attribute names
    $validator = Validator::make($data, $rules, $messages, $attributes);

    // Also not working
    // $validator->setAttributeNames($attributes);

    return $validator;
}

When there's a validation error, the user get's a message like name: Please use fewer characters.. That means the message from my custom array is displayed, but the default attribute name is used. What's wrong here?

Upvotes: 1

Views: 2345

Answers (3)

Michael Nguyen
Michael Nguyen

Reputation: 1752

use Laravel Form Request, scroll down to Customizing The Error Messages section. Check out the below sample code.

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UserRegistrationForm extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'name' => 'required|max:20',
        ];
    }

    /**
     * Get the error messages for the defined validation rules.
     *
     * @return array
     */
    public function messages()
    {
        return [
            'name.max' => 'Please use less characters'
        ];
    }
}

In controller

public function register(UserRegistrationForm $request)
    {
         // do saving here
    }

Upvotes: 1

Jack
Jack

Reputation: 787

Attributes do not replace key names, they are used to change the appearance of a key within a message - i.e The Name field is required - to achieve what you're trying to do in your question you'll need to create a new data array.


protected function validator(array $data)
{
    $data = [
        'Name' => $data['name'] ?? '',
        // ...
    ];

    // ...

    Validator::make($data, $rules, $messages);
}

Upvotes: 1

r0ulito
r0ulito

Reputation: 487

That is coming from validation.php located in resources/Lang/xx/

EDIT :

You'll have to use

$messages = [ 'name.max' => 'Your sentence here', ];

Upvotes: 0

Related Questions