首页 > 解决方案 > 从验证 MessageBag 中替换消息数组

问题描述

我有一个扩展 FormRequest 类的类,我想用新的消息数组替换整个消息数组

这是我成功获取旧消息但现在想用$uniqueMessages 数组替换现有 MessageBag 数组的方法

/**
 * Configure the validator instance.
 *
 * @param  \Illuminate\Validation\Validator  $validator
 * @return void
 */
public function withValidator($validator)
{
    $validator->after(function ($validator) {
        $validationMessages = $validator->errors()->getMessages();
        $uniqueMessages = array_unique($validationMessages, SORT_REGULAR);
        //Require Stuff that set  $uniqueMessages array to the MessageBag
    });
}

我知道我们可以使用它添加额外的消息$validator->errors()->add('field', 'Something is wrong with this field!');

但我想用新消息替换现有消息

标签: phplaravellaravel-validation

解决方案


推荐阅读