1

I've just started to test out Laravel. I'm using a form with some fields and trying to validate the inputs using Laravel's built-in validator class.

$input = Input::all();
$rules = array(
        'fname' => 'required|max:100',
        'lname' => 'required|max:100',
        'email' => 'required|email',
            );
$validation = Validator::make($input, $rules);
if ($validation->fails()){
            return Redirect::to('inputform')
                            ->with('input_errors', $validation->errors);
            }

Everything goes well, and the validation check works. When validation fails, I put the errors in a session variable called input_errors and pass it to the view. My problem is that I can't seem to display the errors. I tried a foreach loop using the blade templating engine as given below:

@foreach (Session::get('input_errors') as $message)
    {{ What Should I put here? }}
@endforeach

How can I display the errors that are being returned as an array. I tried referencing it as $message[0][0] but it didn't work.

Thanks.

EDIT: Sorry, forgot to mention that I'm using Laravel 3

1 Answer 1

4

The correct syntax for getting the errors is...

$messages= $validation->messages();

That alone, unfortunately, is not going to return you the messages. It's going to return a MessageBag instance. This allows you to pull out any specific messages you want or all.

If you want to get all the messages, now you can do do...

$errors = $messages->all();

That will return an array you could loop through in your view to display errors. There are also methods for getting errors on a specific field such as...

$firstNameError = $messages->first('fname');

or

$firstNameErrors = $messages->get('fname');

I also suggest when sending error messages to the view, to use...

->with_errors($validation);

That will flash the errors to session and automatically assume you are sending them as an $errors variable. Then you may display the errors in your view with.

{{ $errors->first('fname') }}  // Blade approach
<?php echo $errors->first('email'); ?> // Non-blade approach

This way, you don't have to add logic to your views trying to determine if the variable exists before you should try and echo it.

http://four.laravel.com/docs/validation

Sign up to request clarification or add additional context in comments.

6 Comments

I get an error message when using withErrors, saying it is undefined function
@rahules Should work. What's the actual code you're using now? Is your Laravel up-to-date?
It worked. I had to use with_errors instead of withErrors. I am using laravel 3.2.14 actually. Accidentally had laravel-4 as tag. Changed it and updated post.
Okay, I changed the answer to match what worked for you so people are not confused with the snake_case vs camelCase issue in Laravel 3/4.
Thanks. A small doubt though. Is the semicolon after the blade statements a standard? or is it just a matter of preference?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.