Laravel 5.1:无法在模板中显示 $errors->first
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33490889/
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
Laravel 5.1: Unable to display $errors->first in template
提问by Simpanoz
I have following html:
我有以下 html:
<div class="form-group
@if( $errors->has('question') )
has-error has-feedback
@endif
">
<label for="question" class="col-md-4 control-label">Question</label>
<div class="col-md-6">
<input type="text" class="form-control c-square c-theme"
id="Idquestion" name="question" value="{{Request::old('question')}}" placeholder="question text" >
@if ($errors->has('question'))
<span class="glyphicon glyphicon-remove form-control-feedback">
{!! $errors->first('question') !!}
</span>
@endif
<?php echo 'PHP ERRORS: '. print_r ($errors->first('question')) ; ?>
@if( $errors->has('question'))
<span class="glyphicon glyphicon-remove form-control-feedback">
{{ $errors->first('question') }}
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="answer" class="col-md-4 control-label">Answer</label>
<div class="col-md-6">
<input type="text" class="form-control c-square c-theme" id="Idanswer" name="answer" placeholder="input">
</div>
</div>
My problem is that, only $errors->all() is working. But $errors->has('question'), $errors->first('question') not working.
我的问题是,只有 $errors->all() 有效。但是 $errors->has('question'), $errors->first('question') 不起作用。
Following is my error showing html code:
以下是我显示 html 代码的错误:
@if (count($errors) > 0)
<div class="c-center alert alert-danger" role="alert">
<ul>
<li>Errors: {{ print_r ($errors) }}</li>
<li>Total Errors: {{ count($errors) }}</li>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
<li>Question Error: {{ $errors->first('question') }} </li>
</ul>
</div>
@endif
Html code for errors, shows me following:
错误的 Html 代码,向我显示以下内容:
Errors: Illuminate\Support\ViewErrorBag Object ( [bags:protected] => Array ( [default] => Illuminate\Support\MessageBag Object ( [messages:protected] => Array ( [0] => Array ( [0] => The question field is required. ) [1] => Array ( [0] => The answer field is required. ) ) [format:protected] => :message ) ) ) 1
Total Errors: 2
The question field is required.
The answer field is required.
Question Error:
Can some one guide me what I am doing wrong and how it can be rectified.
有人可以指导我做错了什么以及如何纠正。
回答by Jobin Jose
Try this,
尝试这个,
on your blade something link below.
在你的刀片上有下面的链接。
<div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
<div class="form-group {{ $errors->has('name') ? 'has-error' : '' }} control-required">
{!! Form::label('title', 'Title') !!}<span class="mand_star"> *</span>
{!! Form::text('title', isset($news->title) ? $news->title : \Input::old('title'), [
'class' => 'form-control',
'placeholder' => 'News Title',
'required' => 'required'
]) !!}
<span class="error_span"> {{ $errors->first('title') }}</span>
</div>
</div>
On your controller something like.
在您的控制器上类似。
if ($validation->fails()) {
return redirect()->route('your route path')->withErrors($validation)->withInput();
}
it works for all individual input fields also fills old data in that field.
它适用于所有单独的输入字段,还会填充该字段中的旧数据。
hope it helps.
希望能帮助到你。
回答by ArtisanBay
Laravel Error Bag
Laravel 错误包
Form name could be passed to the MessageBag
of errors, allowing you to retrieve the error messages for a specific form.
表单名称可以传递给MessageBag
错误,允许您检索特定表单的错误消息。
Pass a name as the second argument to withErrors
:
将名称作为第二个参数传递给withErrors
:
return redirect('viewName')->withErrors($validator, 'formName');
In the view(blade) you can access the MessageBag
using the name:
在视图(刀片)中,您可以MessageBag
使用以下名称访问:
{{ $errors->formName->first('formFieldName') }}
Remember that you pass the name of the field inside first(). Check if you have form element with name "question"
请记住,您在first() 中传递了字段的名称。检查您是否有名称为“问题”的表单元素
Refer docs Laravel Error Bag
参考文档Laravel 错误包
Hope this is helpful.
希望这是有帮助的。