如何在数组输入验证中设置自定义错误消息? [英] How to set custom error message on array input validation?

查看:45
本文介绍了如何在数组输入验证中设置自定义错误消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

字段category.0是必需的,我试图更改 validation.php 文件中的 custom 数组,如下所示:

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'
    ],

但是它没有任何改变.

推荐答案

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

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

这篇关于如何在数组输入验证中设置自定义错误消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆