0

I'm trying to access the messages from the validator in the controller using the following code.

    $valid = Validator::make($input,$rules);
    print_r($valid->messages());

However, I'm not getting any output even though the validation is failing.

What do I need to do to get the Laravel-generated validation messages?

2
  • 1
    Strange. try this $failed = $valid->failed(); dd($failed); Commented Dec 1, 2013 at 8:02
  • It should work. If you get no output are you sure that your to your controller route is proper configuring ? Commented Dec 1, 2013 at 9:58

1 Answer 1

1

You need to run $validator->fails() or $validator->passes() before checking for the messages:

$valid = Validator::make($input,$rules);
$validator->fails();
print_r($valid->messages());

But assuming you know that, well, you could be using messages()->all(), but the way you're are doing should work. Try this (standalone-ish) code in your end:

$validator = Validator::make(
    array('name' => 'Dayle'),
    array('name' => 'required|min:15')
);

if ($validator->fails())
{
    // The given data did not pass validation
}

print_r($validator->messages()->all());

If it works then the problem is realated to your $input and/or $rules.

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

1 Comment

That works. The issue was I wasn't running the fails() method first!

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.