Сообщение проверки вложенного массива Laravel - PullRequest
0 голосов
/ 13 ноября 2018

У меня есть вложенный массив json, который мне нужно проверить.Я пытаюсь дать пользовательские сообщения проверки для полей в массиве.Я просмотрел документы и некоторые посты, но до сих пор не могу понять это.Правила, которые я указал:

    return [
        'member_id'                 => 'required|exists:member,id',
        'payment_method'            => 'required',
        'items.*.products.*.id'       => 'required|exists:product,id',
        'items.*.products.*.quantity' => 'required|integer|min:1',
        'items.*.packages.*.id'       => 'required|exists:package,id',
        'items.*.packages.*.quantity' => 'required|integer|min:1'
    ];

В моих сообщениях функция

   public function messages(){
        return [
            'custom' => [
                'items.*.products.*.id' => [
                    'required'  => 'Product ID is required.',
                    'exists' => 'Selected product invalid.',
                ],
            ],
        ];
   }

Однако мое сообщение проверки все еще: The selected items.0.products.0.id is invalid.

Ответы [ 2 ]

0 голосов
/ 13 ноября 2018

Попробуйте это:

   public function messages(){
        return [
            'items.*.products.*.id.required' => 'your message here',
            'items.*.products.*.id.exists' => 'your message here',
            'items.*.products.*.id.exists' => 'your message here',
            'items.*.products.*.quantity.required' => 'your message here',
            'items.*.products.*.quantity.integer' => 'your message here',
            'items.*.products.*.quantity.min' => 'your message here',
            // etc... you get the idea
        ];
   }

См .: https://laravel.com/docs/5.7/validation#custom-error-messages Указание пользовательского сообщения для данного атрибута

0 голосов
/ 13 ноября 2018

Вы пробовали использовать без ключа custom вот так:

public function messages(){
    return [
         'items.*.products.*.id' => [
                'required'  => 'Product ID is required.',
                'exists'    => 'Selected product invalid.',
         ]
    ];
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...