1
votes

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?

2
Where is this code? Are the rules public?Ted
It's in a controller... in public function post_new()markstewie
Try reducing to just required.Ted
Reduced to just... 'name' => 'required'. Same problem ... $v->errors = NULLmarkstewie

2 Answers

3
votes

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

if ($v->fails()) {
  dd($v->errors);
}
2
votes

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());