DonnaJo
DonnaJo

Reputation: 538

Laravel 5 Validation Not Catching Errors

I am too much of newb to figure out how to track this down properly, so hoping someone here can help me.

The Validator no longer works in my Laravel 5 app. It does not catch errors. Everything passes. I have narrowed down my code so that I am doing a simple die and dump on the validator on form submission, like so:

public function postRegister(Request $request){
    $validator = $this->registrar->validator( $request->all() );
    dd($validator);
}

The Registrar validator method is:

public function validator( array $data )
{
    return Validator::make( $data, [
        'username'   => array('required, regex:/^[a-zA-Z0-9\-\s]+$/, max:20'),
        'email'      => 'required|email|max:255|unique:users',
        'password'   => 'required|confirmed|min:6',
        'honeyfield' => 'size:0',
        'unhuman'    => 'boolean:0',
        'honeytime'  => 'honeytimer:7'
    ] );
}

(The 'honeytimer' is a custom validation set up with a Validator Service Provider).

Now, if I submit an error-filled form, I get back a Validator object with all of the rules defined, but an empty #failedRules array, and if I die and dump $request, there is no error bag.

If I die and dump $validtor->fails(), it returns true, but when I try to retrieve the array with $validator->failed(), it is empty.

Getting into further detail (with special thanks to @shaddy for helping to flush this out) the regular method (below) checks first for whether or not the validation failed, but it does not appear to be registering that it has, because it simply continues on, and attempts to create the user anyway.

public function postRegister( Request $request )
{
    $validator = $this->registrar->validator( $request->all() );

    if( $validator->fails() ) {
        $this->throwValidationException(
            $request, $validator
        );
    }

    $this->auth->login( $this->registrar->create( $request->all() ) );

    \Session::flash( 'new_registration', true );

    return redirect()->intended( '/' );
}

It was all working properly when it was first implemented. So, it's clearly some change I've made in the last couple of days to some other part of the app that is causing this. Naturally, I've tested everything I could think of (in terms of changes I made), but nothing seems to be making any difference.

Could this have something to do with the order in which the classes are being loaded? What am I missing here? I'm sure it's something obvious, but I could really use some advice. Thanks in advance.

Upvotes: 0

Views: 3540

Answers (4)

DonnaJo
DonnaJo

Reputation: 538

Thanks to all for your help! For anyone who runs into this in the future, it turns out that placing the regex rule in an array, as suggested (though not required) by the Laravel 5 documentaion here: http://laravel.com/docs/5.0/validation#rule-regex, was - in my case - the culprit. Changing it back to pipe delimiters solved the problem.

So, changing:

'username'   => array('required, regex:/^[a-zA-Z0-9\-\s]+$/, max:20')

to...

'username' => 'required|regex:/^[a-zA-Z0-9\-\s]+$/|max:20'

...caused the validation to work correctly. It is possible that I did not have the array set up properly, of course. If this is the case, input is appreciated. Either way, the pipe delimiting did the trick.

EDIT:

@Codel96 nailed this in their answer. I had not set up the array properly in the first place.

Upvotes: 0

Madnx
Madnx

Reputation: 1564

You're supposed to have something like

array('required', 'regex:...')

instead of

array('required, regex...')

which does not make any sense. Each one of your validation rules should either be delimited by pipelines or be a single column in an array.

Upvotes: 1

Sh1d0w
Sh1d0w

Reputation: 9520

The failed array is empty, because you just create new instance of the validator. You do not actually trigger the validation.

So you can do this to trigger the validation and check if it fails:

public function postRegister(Request $request){
    $validator = $this->registrar->validator( $request->all() );
    dd($validator->fails());
}

You may also access an array of the failed validation rules, without messages. To do so, use the failed method:

$failed = $validator->failed();

You can view more examples in the documentation.

EDIT:

Now after I see you whole code I can say that you need to handle the situation, where you have errors:

public function postRegister( Request $request )
{
    $validator = $this->registrar->validator( $request->all() );

    if( $validator->fails() ) {
        // Validator fails, return to the previous page with the errors
        return Redirect::back()->withInput()->withErrors( $validator );
    }

    $this->auth->login( $this->registrar->create( $request->all() ) );

    \Session::flash( 'new_registration', true );

    return redirect()->intended( '/' );
}

Upvotes: 0

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5445

after validation you need to catch validation error message

if ($validator->fails()):
        return $validator->messages()->first();

Upvotes: 0

Related Questions