用于数组元素的自定义laravel验证程序?

我想自定义我的Laravel验证器。

截至目前,我有多个复选框

<input type="checkbox" name="prodRelatedID[]" value="1">
<input type="checkbox" name="prodRelatedID[]" value="5">
<input type="checkbox" name="prodRelatedID[]" value="189">

and in my Request.php file I have this

public function rules()
{
    return [
        'prodTitle' => ['required', 'string', 'max:255'],
        'prodDesc' => ['required', 'string', 'max:255'],
        'attachment' => ['image','mimes:jpeg,png,jpg,gif,svg','max:2048'],
        'prodSize' => ['required','string','max:255'],
        'prodCategory' => ['required','string','max:255'],
        'prodPrice' =>  ['required','regex:/^\d*(\.\d{1,2})?$/'],
        'prodRelatedID' => ['required'],
        'prodRelatedID.*' => ['accepted'],
    ];
}

Now how can I customize the error message like for the prodRelatedID ?

如果用户未选中复选框,则将返回如下内容

Please choose atleast 1 product related

评论
  • 一脸B相
    一脸B相 回复

    如果您使用表单请求,则可以覆盖messages方法:

    public function messages()
    {
        return [
            'prodRelatedID.required' => 'Please choose at least 1 product related',
        ];
    }