如何在数组输入验证上设置自定义错误消息?

时间:2015-12-29 15:38:34

标签: php validation laravel

在我的表单中有动态选择,我通过Laravel 5使用Request类验证它们:

$rules = [
   'address' => 'sometimes|required|max:70',
   'area' => 'sometimes|required|numeric|max:10000',
];
foreach ($this->request->get('category') as $key => $val) {
    $rules['category.' . $key] = 'sometimes|required|exists:categories,id';
}

但验证错误我无法更改并显示如下内容:

Field category.0 is required,我尝试更改custom文件中的validation.php数组,如下所示:

'custom' => [
    'category.0' => [
        'required' => 'Category is required field'
    ],

但它没有改变任何东西。

1 个答案:

答案 0 :(得分:1)

为数组输入设置自定义验证消息(在我的情况下选择) 我在messages()类中使用了Request方法:

public function messages(){
    $messages = [];
    foreach ($this->request->get('category') as $key => $val) {
        $messages['category.'.$key.'.required'] = 'Выберите подраздел';
    }
    return $messages;
}