php - How to give Laravel 6 validation attributes a custom name for error message?

194

I am trying to change the way my errors display when my Laravel form is not filled in correctly. Currently, when I get an error. It displays like this.

name mag niet groter zijn dan 255 karakters.

Because this language is Dutch, I would like to change the 'name' attribute to 'naam'. I have tried to change the$attributesNames like this but unfortunately it did not work.

$attributeNames = [
   'name' => 'Naam'   
];

This is what my validation function currently looks like.

/**
 * @return array
 */
public function validateCampaign() {
    // name needs to render as 'Naam'

    return request()->validate([
        'name' => 'required|max:255',
    ]);
}
170

Answer

Solution:

As it turns out. I needed to edit the'attributes' array in myresources/lang/xx/validation.php file.

It turns out like this:

'attributes' => [
    'name' => 'Naam'
]
66

Answer

Solution:

hey i found similar issue like you maybe this will help

maybe like this

    $attributeNames = array(
        'name' => 'Naam',  
    );

    $validator = Validator::make ( request()->all(), [
        'name' => 'required|max:255',
    ]);
    $validator->setAttributeNames($attributeNames);

and you can see more about this in laravel documentation

636

Answer

Solution:

There is an easy way to set up custom error message:

public function validateCampaign() {
    // name needs to render as 'Naam'

    return request()->validate([
        'name' => 'required|max:255',
    ],[
        'name.required' => 'Namm is required',
        'name.max' => 'Namm must be max 255 length'
    ]);
}

People are also looking for solutions to the problem: php - How can I bootstrap a Laravel application from within a Laravel application?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.