1

This is probably a simple fix... but I can't get validation to work.

I've simplified my test back to this...

    $input = array(
       'name' => ''
    );

    $rules = array(
       'name' => 'required|min:3|max:50|alpha'

    );

    $v = Validator::make($input, $rules);

And even though 'name' is required and has all the other rules the validator doesn't contain any errors.

dd($v->errors); // returns NULL

However

dd($v->fails()); // returns bool(true)

Why are there no error messages? When I dump the whole $v object there are no messages to be seen anywhere. Very confused... help appreciated. Thanks.

---- edit

I've simplified this even further. I've put this directly in a view to test...

<?php

$input = array(
   'name' => ''
);

$rules = array(
   'name' => 'required'
);

$v = Validator::make($input, $rules);

dd($v);

?>

I still get exactly the same problem?

Here is the $v object

object(Laravel\Validator)#32 (9) {

  ["attributes"]=>
  array(1) {
    ["name"]=>
    string(0) ""
  }
  ["errors"]=>
  NULL
  ["rules":protected]=>
  array(1) {
    ["name"]=>
    array(1) {
      [0]=>
      string(8) "required"
    }
  }
  ["messages":protected]=>
  array(0) {
  }
  ["db":protected]=>
  NULL
  ["bundle":protected]=>
  string(11) "application"
  ["language":protected]=>
  NULL
  ["size_rules":protected]=>
  array(4) {
    [0]=>
    string(4) "size"
    [1]=>
    string(7) "between"
    [2]=>
    string(3) "min"
    [3]=>
    string(3) "max"
  }
  ["numeric_rules":protected]=>
  array(2) {
    [0]=>
    string(7) "numeric"
    [1]=>
    string(7) "integer"
  }
}

Is something in my installation/setup broken?

4
  • Where is this code? Are the rules public? Commented Dec 30, 2012 at 1:52
  • It's in a controller... in public function post_new() Commented Dec 30, 2012 at 1:56
  • Try reducing to just required. Commented Dec 30, 2012 at 2:02
  • Reduced to just... 'name' => 'required'. Same problem ... $v->errors = NULL Commented Dec 30, 2012 at 2:05

2 Answers 2

3

You have to test your Validator before there are any errors. Try this:

if ($v->fails()) {
  dd($v->errors);
}
Sign up to request clarification or add additional context in comments.

2 Comments

I knew it would be an easy answer! Thanks very much!
My pleasure. Best practice is to not have your rules in a controller. Try sticking them in a model as a public static array. Easier to maintain. Form classes holding the rules are also popular (one for each form).
2

I think you need to call $v->passes or $v->fails first, for it to actually evaluate your validation rules and generate errors. Then you can use...

dd($v->errors->all());

Comments

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.