Adrenaxus
Adrenaxus

Reputation: 1613

Custom error messages for array input

Consider the following <input> array in my form:

 <input type="text" name="title[1]" value="">
 <input type="text" name="title[2]" value="">
 <input type="text" name="title[3]" value="">

The numbers (1,2,3) refer to different languages. 1 = English, 2 = German, etc.

How can I add custom error messages for an input array?


I have tried the following without success in my app/lang/en/validation.php:

<?php   
    return [
        'custom' => [
            'title.1' => [
                'required' => 'The english title is required.',
            ],
            'title.2' => [
                'required' => 'The german title is required.',
            ],
            'title.3' => [
                'required' => 'The italian title is required.',
            ],                                                                
        ],
    ];
?>

Laravel throws the default error messages instead of using my custom messages:

The title.1 field is required.
The title.2 field is required.
The title.3 field is required.

Thank you for any help you can provide!


EDIT: It works if I pass the message to my validator like this:

$messages = array(
    'title.1.required' => 'The english title is required',
);
$validator = Validator::make($data = Input::all(), $rules, $messages);

But I can't get it to work in the app/lang/en/validation.php file.

Upvotes: 3

Views: 1148

Answers (2)

jay thanki
jay thanki

Reputation: 1450

By using below way i got solution

In Controller

        $input=array (
          'name' => 'pro 1',
          'barcode' => '2222',
          'vendors' => 
          array (
            0 => 
            array (
              'id' => 51,
              'name' => 'v1',
              'item_code' => 'khgjhgjhkhjgjhgjhkhjgjhgjhkhjgjhgjhvv',
            ),
            1 => 
            array (
              'id' => 43,
              'name' => 'v3',
              'item_code' => 'aerfaf132aw1d32aw1d32wad',
            ),
          ),
        ) 
        $validation = Product::validate($input);
          if ($validation != null && $validation != "" && $validation->fails()) {
            $breakline = $validation->messages()->all();
            $message = implode("<br> ", $breakline);
            Log::warning('Admin::ProductsController::store::' . $message);
            return Response()->json('', $message));
          }  

In Model

public static function validate($data) {
  $rule = array(
    'name' => 'required|max:255',
    'barcode' => 'required|max:255',
    'vendors'=>'present|array|size:1,5',
    'vendors.*.item_code' => 'max:6'
  );
  $messages = array(
    'required' => ':attribute field is required.',
    'name.max' => ':attribute may not be greater than :max characters.',
    'barcode.max'=>':attribute may not be greater than :max characters.'
    'size'=>'only allowed one to five vendor.',
  );
  $data = Validator::make($data, $rule, $messages);
  $data->setAttributeNames(array(
    'name' => ucfirst('name'),
    'barcode' => ucfirst('barcode'),
    'vendors.*.item_code' => ucfirst('item code'),
  ));
  return $data;
}

It will show

Item code may not be greater than 6.
Item code may not be greater than 6.

Upvotes: 2

Bogdan
Bogdan

Reputation: 44526

The dot notation is used to access nested array items, but you're using it for an array key. It expects title and 1 to be two distinct array keys nested in one another. That is probably why there's no match for your custom error messages. Instead try this:

return [
    'custom' => [
        'title' => [
            [ 1 => ['required' => 'The english title is required.']],
            [ 2 => ['required' => 'The german title is required.' ]],
            [ 3 => ['required' => 'The italian title is required.']],
        ],
    ],
];

Upvotes: 0

Related Questions