Laravel 会话数组值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16343676/
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:45:06  来源:igfitidea点击:

Laravel Session Array Values

phplaravellaravel-3

提问by rahules

I've just started to test out Laravel. I'm using a form with some fields and trying to validate the inputs using Laravel's built-in validator class.

我刚刚开始测试 Laravel。我正在使用带有一些字段的表单并尝试使用 Laravel 的内置验证器类来验证输入。

$input = Input::all();
$rules = array(
        'fname' => 'required|max:100',
        'lname' => 'required|max:100',
        'email' => 'required|email',
            );
$validation = Validator::make($input, $rules);
if ($validation->fails()){
            return Redirect::to('inputform')
                            ->with('input_errors', $validation->errors);
            }

Everything goes well, and the validation check works. When validation fails, I put the errors in a session variable called input_errorsand pass it to the view. My problem is that I can't seem to display the errors. I tried a foreachloop using the blade templating engineas given below:

一切顺利,验证检查有效。当验证失败时,我将错误放在一个名为的会话变量中input_errors并将其传递给视图。我的问题是我似乎无法显示错误。我尝试foreach使用blade templating engine下面给出的循环:

@foreach (Session::get('input_errors') as $message)
    {{ What Should I put here? }}
@endforeach

How can I display the errors that are being returned as an array. I tried referencing it as $message[0][0]but it didn't work.

如何显示作为数组返回的错误。我尝试将其引用为,$message[0][0]但没有奏效。

Thanks.

谢谢。

EDIT: Sorry, forgot to mention that I'm using Laravel 3

编辑:抱歉,忘了提及我正在使用 Laravel 3

回答by user1669496

The correct syntax for getting the errors is...

获取错误的正确语法是...

$messages= $validation->messages();

That alone, unfortunately, is not going to return you the messages. It's going to return a MessageBaginstance. This allows you to pull out any specific messages you want or all.

不幸的是,仅凭这一点,不会给您回复消息。它将返回一个MessageBag实例。这允许您提取您想要的任何特定消息或全部。

If you want to get all the messages, now you can do do...

如果你想得到所有的消息,现在你可以做...

$errors = $messages->all();

That will return an array you could loop through in your view to display errors. There are also methods for getting errors on a specific field such as...

这将返回一个数组,您可以在视图中循环以显示错误。还有一些方法可以在特定字段上获取错误,例如...

$firstNameError = $messages->first('fname');

or

或者

$firstNameErrors = $messages->get('fname');

I also suggest when sending error messages to the view, to use...

我还建议在向视图发送错误消息时,使用...

->with_errors($validation);

That will flash the errors to session and automatically assume you are sending them as an $errorsvariable. Then you may display the errors in your view with.

这会将错误闪烁到会话中并自动假设您将它们作为$errors变量发送。然后你可以在你的视图中显示错误。

{{ $errors->first('fname') }}  // Blade approach
<?php echo $errors->first('email'); ?> // Non-blade approach

This way, you don't have to add logic to your views trying to determine if the variable exists before you should try and echo it.

这样,您就不必在视图中添加逻辑来尝试确定变量是否存在,然后再尝试回显它。

http://four.laravel.com/docs/validation

http://four.laravel.com/docs/validation