我在控制器的操作中有以下验证:
foreach ($request['qtys'] as $key => $val){
if (!$this->_validateMinQty($key, $job, $val)){
$customerTitle = $job->customers()->where('customer_id',$key)->first()->title;
return redirect()->back()->withErrors(['qtys' => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
}
}
在视图中检查多个表单的名为qtys
的输入:
@foreach($job->customers as $customer)
<div class="form-group {{$errors->first('qtys has-error')}}">
{!! Form::label('qtys-'.$customer->id, __('Qty').' '.$customer->title) !!}
<div class="row">
<div class="col-md-9">
{!! Form::text('qtys['.$customer->id.']',$customer->pivot->e_production,['class' =>'form-control qtys', "data-sumequal"=>"qty",'required' => 'required','title' => $customer->pivot->aid,'id' => 'qtys-'.$customer->id]) !!}
<div class="help-block with-errors"></div>
@php ($eleE = $errors->first('qtys'))
@include('layouts.form-ele-error')
</div>
<div class="col-md-3">
<a href="/storage/create/{{$customer->pivot->aid}}" class="btn btn-nile"><i class="fox-add"></i>{{__('Add Storage')}}</a>
</div>
</div>
</div>
@endforeach
上述代码有效,但有以下限制:
错误消息在每个名为qtys[x]
的输入下呈现,其中x是一个整数,第一个输入Testana
具有无效的数量,如下面的屏幕截图所示:
在控制器的操作返回消息中,我尝试使用索引名称作为输入,如下所示:
return redirect()->back()->withErrors(['qtys.10' => ....
但是,它会阻止在任何qtys
字段下呈现错误消息。有没有解决方案?
答案 0 :(得分:1)
我找到的解决方案从视图中的definition of first method开始:
@php ($eleE = $errors->first('qtys'))
在我的代码中,这应该改为:
@php ($eleE = $errors->first('qtys.'.$customer->id))
因为多个字段的密钥等于客户ID。 当我想以单个帖子或单个表单元素发送双重数据时,这是我经常使用的技术。 然后在控制器中,我保持第一次尝试,
return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
$key
是整数。