Russ Back
Russ Back

Reputation: 923

Detecting type of validation error in Laravel 4

I'm new to Laravel and the following works but it doesn't seem very 'Laravel' to me - I just need to detect which validation rule the message refers to (required,email,unique etc):

@if ($errors->has('email'))
    {{ $errors->first('email') }}
    @if (strpos($errors->first('email'), 'has already been taken'))
        {{ HTML::link('password', 'Need a reminder?', array(), FALSE); }}
    @endif
@endif

Any suggestions?

Thanks

Upvotes: 1

Views: 775

Answers (1)

Laurence
Laurence

Reputation: 60048

In your controller, would would do something like this (depending on how it is configured)

Controller

public function store()
{
    $validator = Validator::make(Input::all(), array(
                      'name' => 'Dayle',
                      'email' => 'required|min:5'
                   ));

    if ($validator->passes())
    {
         // Redirect to success page or something
    }

    return Redirect::back()
             ->withInput()
             ->withErrors($validator)
             ->withFailed($validator->failed())
}

then in your view

View

@if ($errors->has('email'))
    The specific email rule that failed was: {{ $failed['email'] }}
@endif

Upvotes: 1

Related Questions