【问题标题】:Laravel not responding with validator errorsLaravel 没有响应验证器错误
【发布时间】:2017-10-21 08:21:06
【问题描述】:

我验证了一个模型

$validator = $c->validate($collection);

这是验证函数

public function validate($data){
    return Validator::make($data, $this->rules());;
}

这些是规则

public function rules() {

    return  array([
        'name' => [
            'required', 'You need to choose a name for your collection.',
            'unique:collections,table_name', 'A collection or collection table with this name already exists'
        ],
           ...
        ]);
}

我正在尝试发回带有验证器错误的 JSON 响应,例如:

return response()->json($validator->errors(), 200);     

我目前正在测试“名称”规则的验证,但验证器失败了,正如预期的那样。

但是,我希望它返回该规则的消息(“具有此名称的集合或集合表已存在”)

相反,我将其返回:

我的目标是让 laravel 发回我需要的错误,提前感谢您的帮助。


编辑:更新代码:

消息:

public function messages(){
return [
'name.required' => 'A name must be specified for the collection',
'name.unique' => 'A collection or collection table with this name already exists',
'name.min' => 'The collection name is too short',
'fields.*.fieldName.unique' => 'Field names must be unique',
'fields.*.fieldName.required' => 'One or more fields must be specified for the collection',
'fields.*.fieldName.not_in' => 'Illegal field name, please try another one',
'fields.*.fieldName.min' => 'The field name is too short',
'fields.*.dataType.required' => 'A data-type must be specified for fields',
'fields.*.dataType.in' => 'Illegal data-type'
];

}

public function rules() {

    return  array([
        'name' => [
        'required', 'You need to choose a name for your collection.',
        'unique:collections,table_name', 'A collection or collection table 
with this name already exists',
        'min:2'
        ],
        'fields.*.fieldName' =>  
        [       
        'unique' => 'Please ensure that the fields are uniquely named.',
        'required' => 'You must specify a name for your fields.',
        'not_in:'.implode(',', self::$illegalFieldNames),
        'min:2'
        ],

        'fields.*.dataType' =>
        [
        'required', 'You must specify a data type for your fields.',
        'in:'.implode(',', self::$allowedDataTypes)
        ]

        ]);
}


public function validate($data){

    return Validator::make($data, $this->rules(), $this->messages());
}

【问题讨论】:

    标签: json laravel validation response rules


    【解决方案1】:

    验证器make 方法将第三个参数作为消息数组。你不能像那样混用规则和消息。

    public function rules()
    {
        return [
            'name' => 'required|unique:collections,table_name'
        ];
    }
    
    public function messages()
    {
        return [
            'name.required' => 'You need to choose a name for your collection',
            'name.unique' => 'A collection or collection table with this name already exists',
        ];
    }
    
    public function validate($data)
    {
        return Validator::make($data, $this->rules(), $this->messages());
    }
    

    【讨论】:

    • 很好。但是我如何才能知道实际返回了哪个错误/消息?
    • Laravel 让你可以完全控制错误信息。查看文档以获取更多信息laravel.com/docs/5.4/validation#working-with-error-messages
    • 哦,那么,当违反特定规则时,会自动返回“field.rule”索引的消息值?
    • @BarryD。是的。您需要为特定输入的每个规则定义自定义消息。如果需要,您也可以为输入中的所有错误定义一条消息。
    • 好吧,我认为我的消息是有条理的,但我仍然有原来的问题 - 我想要的消息没有被检索到。我只是得到 0 字段之一。
    【解决方案2】:
      $this->rules($request, array( 
    
                'name' => 
      'required|alpha_dash|min:5|max:255|unique:posts
    
    
                ));  
    

    使用java脚本来显示错误

    或者你可以使用类似这样的东西。

    public function store(Request $request)
    
    $validator = Validator::make($request->all(), [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ]);
    
        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }
    
        // Store the blog post...
    }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-11-18
      • 2020-09-04
      • 2016-05-07
      • 2017-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-18
      • 2018-11-04
      相关资源
      最近更新 更多