How to set custom error message on array input validation?

主宰稳场 提交于 2020-12-12 12:21:20

问题


in my form there is dynamic selects and I validate them via Laravel 5 usin Request class:

$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';
}

But validation errors I can't change and displays something like this:

Field category.0 is required, I tried to change custom array in validation.php file like this:

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

But it didn't change anything.


回答1:


To set custom validation message for array input (select in my case) I used messages() method in my Request class:

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


来源:https://stackoverflow.com/questions/34514215/how-to-set-custom-error-message-on-array-input-validation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!