0

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',
    ]);
}
Niels Bosman
  • 826
  • 1
  • 8
  • 19

4 Answers4

1

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

It turns out like this:

'attributes' => [
    'name' => 'Naam'
]
Niels Bosman
  • 826
  • 1
  • 8
  • 19
0

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

good112233
  • 144
  • 1
  • 10
0

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'
    ]);
}
Martin
  • 101
  • 6
0

Combining previous examples, you could do this...

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

    return request()->validate(
        ['name' => 'required|max:255'],  // rules
        [],                              // messages
        ['name' => 'Naam']               // custom attributes
    );
}

The Laravel 6 implementation details of the validate function can be found here.

w5m
  • 2,286
  • 3
  • 34
  • 46