具有相同名称的多个字段的Laravel自定义验证消息
我在控制器的操作中进行了以下验证:具有相同名称的多个字段的Laravel自定义验证消息,laravel,laravel-5,laravel-5.4,Laravel,Laravel 5,Laravel 5.4,我在控制器的操作中进行了以下验证: foreach ($request['qtys'] as $key => $val){ if (!$this->_validateMinQty($key, $job, $val)){ $customerTitle = $job->customers()->where('customer_id',$key)->first()->title;
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
字段下呈现错误消息。有解决方案吗?我找到的解决方案从视图中找到的开始:
@php ($eleE = $errors->first('qtys'))
在我的代码中,应将其更改为:
@php ($eleE = $errors->first('qtys.'.$customer->id))
因为多个字段得到的密钥等于客户id。这是我通常使用的一种技术,当我想在单个post或单个表单元素中发送两段数据时。
然后在控制器中,我保持第一次尝试
return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);
其中,$key
是一个整数
return redirect()->back()->withErrors(['qtys.'.$key => __('The qty of the customer :customerTitle is less than allowed qty',['customerTitle' => $customerTitle])]);