Laravel custom messages for array validation

后端 未结 10 2455
南笙
南笙 2021-02-20 05:36

I am having a form and I have an array of input fields for video urls, now when I validate form if I have multiple invalid fields with video urls, I get the same message for eac

10条回答
  •  暖寄归人
    2021-02-20 06:08

    You can using Customizing The Error Format

    protected function formatErrors(Validator $validator)
    {
        $results = [];
        $flag = false;
        $messages = $validator->errors()->messages();
        foreach ($messages as $key => $value) {
            if (!str_contains($key, 'external_media') || !$flag) {
                $results[] = $value[0];
            }
            if (str_contains($key, 'external_media') && !$flag) {
                $flag = true;
            }
        }
        return $results;
    }
    

提交回复
热议问题