Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Validation: check why validator failed

If there a way to check whether or not the validator failed specifically because of 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()) {

In laymans terms, I basically want to know: "did the validation fail because the email_address was not unique?"

like image 790
dcolumbus Avatar asked Aug 29 '14 17:08

dcolumbus


People also ask

How does a validator fail?

Validation errors typically occur when a request is malformed -- usually because a field has not been given the correct value type, or the JSON is misformatted.

How does laravel validation work?

The validate method accepts an incoming HTTP request and a set of validation rules. If the validation rules pass, your code will keep executing normally; however, if validation fails, an exception will be thrown and the proper error response will automatically be sent back to the user.

What is confirmed in laravel validation?

confirmed. The field under validation must have a matching field of foo_confirmation . For example, if the field under validation is password , a matching password_confirmation field must be present in the input.


1 Answers

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

if ($validator->fails()) {

    $failedRules = $validator->failed();

    if(isset($failedRules['email_address']['Unique'])) {

    ...
like image 116
im_brian_d Avatar answered Oct 08 '22 02:10

im_brian_d