Validator Validation: Verify why the validator failed the validation

If there is a way to check if the validator was unsuccessful due to the unique rule?

 $rules = array( 'email_address' => 'required|email|unique:users,email', 'postal_code' => 'required|alpha_num', ); $messages = array( 'required' => 'The :attribute field is required', 'email' => 'The :attribute field is required', 'alpha_num' => 'The :attribute field must only be letters and numbers (no spaces)' ); $validator = Validator::make(Input::all(), $rules, $messages); if ($validator->fails()) { 

Under laymans, I basically want to know: "Did the verification fail because the email_address was not unique?"

+5
source share
2 answers

Check for a specific rule in the returned array of failed rules

 if ($validator->fails()) { $failedRules = $validator->failed(); if(isset($failedRules['email_address']['Unique'])) { ... 
+12
source

An error message will appear and inform you that it failed:

controller

 if($validation->fails()){ return Redirect::back()->withErrors($validation)->withInput(); } foreach($errors->all() as $error) { echo $error; } 

And in your blade template add this:

  @foreach($errors->all() as $error) <div> {{$error}} </div> @endforeach 

And this will return a message with any error. Email does not match. Required field. Blah blah

You can also remove this array of addresses from the $ message. The validator will handle everything for you. You only want to use this if you want to customize messages.

You can also try var_dump this expression:

var_dump ($ validation-> errors ()); are dying;

+1
source

Source: https://habr.com/ru/post/1201437/


All Articles