[英]Validation in dynamic form Laravel 5.5
如何为动态表单创建验证?
//Controller
public function store(Request $request)
{
$rules = [
'companyName' => 'required',
'bannerName' => 'required',
'bannerDescription' => 'required',
'bannerURL' => 'required',
'bannerImg' => 'required',
];
$customMessages = [
'companyName.required' => 'Yo, what should I call you?',
'bannerName.required' => 'Yo, what should I call you?',
'bannerDescription.required' => 'Yo, what should I call you?',
'bannerURL.required' => 'Yo, what should I call you?',
'bannerImg.required' => 'Yo, what should I call you?',
];
$this->validate($request, $rules, $customMessages);
}
这是我的观点,我的“名称” attr是数组,因为我会将大量数据存储到DB。 单击“ +添加横幅” jquery将使用相同的4个输入克隆div。 如果我删除数组,一切都会正常工作,但是如果没有,我将得到以下错误
htmlspecialchars()期望参数1为字符串,给定数组
{!! Form::open(['action' => 'CompanyController@store', 'method' => 'POST', 'enctype' => 'multipart/form-data']) !!}
<div class="form-group{{ $errors->has('companyName') ? ' has-error' : '' }}">
<label for="companyName">Company URL Address</label>
<input type="text" class="form-control" value="{{old('companyName')}}" id="companyName" name="companyName" placeholder="example.com">
<small class="text-danger">{{ $errors->first('companyName') }}</small>
</div>
<hr>
<div data-sel-baner-box>
<div data-sel-baner-form>
<div class="panel-heading"><h4 style="text-align: center">New banner</h4></div>
<div class="form-group{{ $errors->has('bannerName') ? ' has-error' : '' }}">
<label for="bannerName">Title</label>
<input type="text" class="form-control" id="bannerName" value="{{old('bannerName')}}" name="bannerName[]" placeholder="Name">
<small class="text-danger">{{ $errors->first('bannerName') }}</small>
</div>
<div class="form-group{{ $errors->has('bannerDescription') ? ' has-error' : '' }}">
<label for="bannerDescription">Banner Description</label>
<input type="text" class="form-control" id="bannerDescription" value="{{old('bannerDescription')}}" name="bannerDescription[]" placeholder="Description">
<small class="text-danger">{{ $errors->first('bannerDescription') }}</small>
</div>
<div class="form-group{{ $errors->has('bannerURL') ? ' has-error' : '' }}">
<label for="bannerURL">Banner URL</label>
<input type="text" class="form-control" id="bannerURL" value="{{old('bannerURL')}}" name="bannerURL[]" placeholder="URL">
<small class="text-danger">{{ $errors->first('bannerURL') }}</small>
</div>
<div class="form-group{{ $errors->has('bannerImg') ? ' has-error' : '' }}">
<label for="bannerImg">File input</label>
<input type="file" class="form-control-file" id="bannerImg" name="bannerImg[]">
<small class="text-danger">{{ $errors->first('bannerImg') }}</small>
</div>
</div>
<a href="##" data-sel-btn-add-baner class="btn btn-primary">+ Add Banner</a>
<button type="submit" class="btn btn-primary">Save Company</button>
{!! Form::close() !!}
有什么提示吗?
您必须对数组输入验证规则使用点表示法,如下所示:
$rules = [
'companyName.*' => 'required',
'bannerName.*' => 'required',
'bannerDescription.*' => 'required',
'bannerURL.*' => 'required',
'bannerImg.*' => 'required',
];
您可以在此处查看文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.