Laravel没有响应验证器错误

时间:2017-05-21 17:43:35

标签: json laravel validation response rules

我验证了一个模型

$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);     

我正在测试'name'规则的验证,验证器失败,正如预期的那样。

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

相反,我得到了这个回复:

enter image description here

我的目标是让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());
}

2 个答案:

答案 0 :(得分:1)

验证器package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @Configuration @EnableAutoConfiguration **@ComponentScan({"com.example.demo", "controller", "service"})** public class SpringBootMvcExample1Application { public static void main(String[] args) { SpringApplication.run(SpringBootMvcExample1Application.class, args); } } 方法将第三个参数作为messages数组。你不能混合这样的规则和消息。

make

答案 1 :(得分:0)

  $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...
}
}