Laravel 验证未显示错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14087608/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 07:27:40  来源:igfitidea点击:

Laravel validation not showing errors

phplaravel

提问by markstewie

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.

为什么没有错误信息?当我转储整个 $v 对象时,在任何地方都看不到任何消息。非常困惑...帮助表示赞赏。谢谢。

---- 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

这是 $v 对象

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?

我的安装/设置中有什么问题吗?

回答by Ted

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

您必须在出现任何错误之前测试您的验证器。尝试这个:

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

回答by Andrew

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

我认为您需要先调用$v->passes$v->fails,以便它实际评估您的验证规则并生成错误。然后就可以用...

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