gpt4 book ai didi

validation - Laravel MessageBag 错误数组在 View 中为空,但如果我终止脚本则有内容

转载 作者:行者123 更新时间:2023-12-05 08:17:36 24 4
gpt4 key购买 nike

我正在尝试将错误返回给我的 View ,这是我的 Controller TestcategoryController 的一部分

    $rules =array(
'name' => 'required'
);
$validator = Validator::make(Input::all(), $rules);
//process
if($validator->fails()){
return Redirect::to('testcategory/create')->withErrors($validator);
}

在 View 中testcategory/create如果我尝试输出类似

的错误
        @if($errors->any())         
{{ $errors->first('name') }}
@endif

我一无所获。但是如果我 {{dd($errors)}}我明白了

      object(Illuminate\Support\ViewErrorBag)#91 (1) { ["bags":protected]=> array(1) { 
["default"]=> object(Illuminate\Support\MessageBag)#92 (2)
{ ["messages":protected]=> array(1)
{ ["name"]=> array(1) { [0]=> string(27) "The name field is required." } }
["format":protected]=> string(8) ":message" } } }

我遇到错误的唯一方法是终止脚本。我做错了什么?

最佳答案

可能另一个问题是 $errors 没有被保存到 session 变量 $errors 并且 View 中没有显示任何内容。

这是同一问题的示例: http://laravel.io/forum/03-28-2016-errors-variable-empty-after-failed-validation

对我来说,上面链接中定义的解决方案有效。解决方法:在app\Http\Kernel.php移动\Illuminate\Session\Middleware\StartSession::class, 从 $middlewareGroups 到 $middleware

之前

enter image description here

之后

enter image description here

关于validation - Laravel MessageBag 错误数组在 View 中为空,但如果我终止脚本则有内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25323190/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com