简体   繁体   English

Laravel 4 RESTful API-将验证错误传递给View

[英]Laravel 4 RESTful API - Passing Validation Errors to View

I'm working with a team that recently re-coded our project with a RESTful API. 我正在与一个团队合作,该团队最近使用RESTful API重新编码了我们的项目。

Previous I used to pass in validation errors by using: 以前,我曾经使用以下方法传递验证错误:

return Redirect::to('add_clientes_error')->withErrors($validator)->withInput(Input::all());

And in the view I displayed it using (blade): 在视图中,我使用(刀片)显示了它:

{{ $errors->first('name') }}

Now after the re-factoring the view is created with: 现在,在重构之后,将使用以下视图创建视图:

return Redirect::to('add_clientes_error')->withErrors($response['errors'])->withInput(Input::all());

...and I can see the $errors array since I am printing it out on the view which looks like this: ...并且我可以看到$ errors数组,因为我将其打印在如下所示的视图中:

 Illuminate\Support\ViewErrorBag Object ( [bags:protected] => Array ( [default] => Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [0] => Array ( [field] => name [message] => The name field is required. ) ) [format:protected] => :message ) ) ) 1

My question is...how do I echo the variables now because {{ $errors->first('name') }} is empty. 我的问题是...由于{{ $errors->first('name') }}为空,我现在如何回显变量。 I know the message is no longer the "first" element in the array but not sure how to get to the message element. 我知道消息不再是数组中的“第一个”元素,但不确定如何获取消息元素。

Any help would be appreciated! 任何帮助,将不胜感激!

You can use $errors->getBag('default') as the array containing the errors and then retrieve the error items by using normal array notation. 您可以将$errors->getBag('default')用作包含错误的数组,然后使用常规数组表示法检索错误项。 So for example: 因此,例如:

$errors_array = $errors->getBag('default');
$name_error = $errors_array['name'][0]; // to get the first error on the name field

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM