2017-09-02 84 views
1

後如何停止驗證我沒有線索如何使出現第一個錯誤後laravel驗證停止驗證,然後返回只有一個錯誤Laravel第一個錯誤

規則與val_前綴是我的自定義規則做。

我需要爲PESEL場僅顯示錯誤,如果PESEL字段爲空(「必要」規則) 誰能告訴我,我怎麼能達到呢?

$this->validate($request, [ 

    'pesel'=> 'bail|required|val_pesel', 
    'dane_os' => 'required', 
    'id_project' => 'required', 
    'imie' => 'required|val_imie', 
    'nazwisko'=>'required|val_nazwisko', 
    'nazwisko_matki'=>'required|val_nazwisko_matki' 

]); 

我的驗證碼

Validator::extend('val_pesel',function($attribute,$value,$parameters,$validator) 
     { 

     $val = DB::select('select * from `wyborca` where pesel = "'.$value.'" ; '); 
     if(empty($val)) 
     { 
     return false; 
     } 
     else { 
     return true; 
     } 
    }); 
    Validator::extend('val_imie',function($attribute,$value,$parameters,$validator) 
    { 
     $test = $validator->getData(); 
     $pesel = $test['pesel']; 
     $imie = $test['imie']; 




     $val = DB::select('select * from `wyborca` where pesel = "'.$pesel.'" and imie = "'.$imie.'" ; '); 
     if(empty($val)) 
     { 
     return false; 
     } 
     else { 
     return true; 
     } 
    }); 
    Validator::extend('val_nazwisko',function($attribute,$value,$parameters,$validator) 
    { 
     $test = $validator->getData(); 
     $pesel = $test['pesel']; 
     $nazwisko = $test['nazwisko']; 




     $val = DB::select('select * from `wyborca` where pesel = "'.$pesel.'" and nazwisko = "'.$nazwisko.'" ; '); 
     if(empty($val)) 
     { 
     return false; 
     } 
     else { 
     return true; 
     } 
    }); 
    Validator::extend('val_nazwisko_matki',function($attribute,$value,$parameters,$validator) 
    { 
     $test = $validator->getData(); 
     $pesel = $test['pesel']; 
     $nazwisko_matki = $test['nazwisko_matki']; 




     $val = DB::select('select * from `wyborca` where pesel = "'.$pesel.'" and nazwisko_matki = "'.$nazwisko_matki.'" ; '); 
     if(empty($val)) 
     { 
     return false; 
     } 
     else { 
     return true; 
     } 
    }); 
    Validator::extend('vote_exists',function($attribute,$value,$parameters,$validator) 
    { 
     $test = $validator->getData(); 
     $pesel = $test['pesel']; 




     $val = DB::select('select * from `glosy` where pesel = "'.$pesel.'" ; '); 
     if(empty($val)) 
     { 
     return false; 
     } 
     else { 
     return true; 
     } 
    }); 

} 
+0

一切都看起來正確,你可以添加一些你的自定義規則的細節? –

+0

如果'required'失敗或者您期待其他屬性上的驗證規則的其餘部分不能運行,您是否期望'val_pesel'不會運行? –

+0

如何進行兩次驗證檢查?一個是pesel,一個是其他的。如果pesel驗證無效,則返回,否則繼續。 – Jeffrey

回答

-1

如果要在第一條規則失敗,你應該使用bail停止驗證,從文檔

停在第一個驗證失敗 有時你可能報價希望在第一次驗證失敗後停止在屬性上運行驗證規則。要做到這一點,分配保釋規則屬性:

$this->validate($request, [ 
    'title' => 'bail|required|unique:posts|max:255', 
    'body' => 'required', 
]); 

在這個例子中,如果在title屬性所需的規則失敗,唯一的規則將不檢查。規則將按照他們分配的順序進行驗證。

https://laravel.com/docs/5.5/validation

這裏是GitHub的討論 https://github.com/laravel/framework/issues/4789#issuecomment-174837968

-1

重定向後的第一個規則已經失敗,你可以使用「保釋」關鍵字停止驗證。一個例子:

// $arr = ['name of form field'=>'name to be used in error msg'] 

    $arr = ['coverletter'=>'Cover Letter','vitae'=>'CV','certificate'=>'Certificate','tesol'=>'Tesol','photo'=>'Photo']; 

    foreach ($arr as $k=>$v) { 
     if($k !== 'photo') { 

     if(!Session::get($k) || $request->$k) {   

      $this->validate($request, 
       [$k => 'bail|required|mimetypes:application/pdf'], 
       [$k . '.required' => $v . ' is required', 
       $k . '.mimetypes' => $v . ' must be PDF']); 
      } 
     } 
     if($k == 'photo') { 

      if(!Session::get($k) || $request->$k) {   

      $this->validate($request, 
       [$k => 'bail|required|mimetypes:image/jpg,image/jpeg'], 
       [$k . '.required' => $v . ' is required', 
       $k . '.mimetypes' => $v . ' must be of type jpeg']); 
      } 
     } 
    } 

後的第一條規則失敗,這將重定向回你的錯誤消息。