Laravel 5检查字段和规则

时间:2017-11-02 06:18:57

标签: laravel validation

我希望在验证规则不同时更改状态代码,在代码中if(isset($ValidationResponse['email']['required'])){之前签出评论

public function checkCustomerEmail_Method(Request $request){
        $checkCustomerEmail_rules =  ['email'=>'required|exists:userstable,Email];
        $messages = [
                'required'=>trans('general.mandatory',['type'=>'Email']),
                'exists'=>trans('general.field_notexists',['field'=>'Email'])
        ];

        $ValidationResponse = Validator::make($request->input(),$checkCustomerEmail_rules,$messages);
        $userEmail = $request->input('email');
        if($ValidationResponse->fails()){
            ///Here I can work on that condition
            if(isset($ValidationResponse['email']['exists'])){
                $error_status = 204;
            }
             return response()->json(['error'=>true,'errormsg'=>$ValidationResponse->errors()->first(),'success'=>false],$error_status);

         }

 }

1 个答案:

答案 0 :(得分:0)

自从我使用laravel验证以来已经有一段时间了。但你总是可以把它分成2个验证调用。电子邮件存在1,其余为1。

public function checkCustomerEmail_Method(Request $request){
        $checkEmailExists_rules = ['email'=>'exists:userstable,Email];
        $ValidationResponse = Validator::make($request->input(),$checkEmailExists_rules,$messages);
         if($ValidationResponse->fails()){
         {
            // Return you 204 message
         }

        $checkCustomerEmail_rules =  ['email'=>'required|exists:userstable,Email];
        $messages = [
                'required'=>trans('general.mandatory',['type'=>'Email']),
                'exists'=>trans('general.field_notexists',['field'=>'Email'])
        ];

        $ValidationResponse = Validator::make($request->input(),$checkCustomerEmail_rules,$messages);
        $userEmail = $request->input('email');
        if($ValidationResponse->fails()){
            // Return standard validation failure message (422)?

         }

 }