对于可以添加和修改字段的动态形式:
表格()
<input name="gallery[1][title]">
<input name="gallery[1][text]">
.
.
.
<input name="gallery[n][title]">
<input name="gallery[n][text]">用于验证的控制器中的:
'gallery.*.file' => 'nullable|image',
'gallery.*.title' => 'nullable|string',本地化文件中的:
我不知道会有多少人在数组里。
'gallery.*.text' => 'text of gallery 1',
'gallery.*.title' => 'title of gallery 1',我怎么写呢?
我想得到这样的结果:
第一展厅名称 。 。 。 画廊名称n
发布于 2017-10-23 13:18:08
这是一种很麻烦的方法。不幸的是,laravel目前不支持为特定令牌添加通用消息替换器,因此您可以这样做:
在主计长:
$replacer = function ($message, $attribute) {
$index = array_get(explode(".",$attribute),1);
$message = str_replace(":index",$index,$message);
//You may need to do additional replacements here if there's more tokens
return $message;
}
$this->getValidationFactory()->replacer("nullable", $replacer);
$this->getValidationFactory()->replacer("string", $replacer);
$this->getValidationFactory()->replacer("image", $replacer);
$v = $this->getValidationFactory()->make($request->all(), $rules);
if ($v->fails()) {
$this->throwValidationException($request, $v); //Simulate the $this->validate() behaviour
}您还可以在服务提供者中添加替换程序,使它们在所有路由中都可用,但不幸的是,您需要为希望它们可用的每条规则注册它们。
在本地化文件中:
'gallery.*.text' => 'text of gallery :index',
'gallery.*.title' => 'title of gallery :index',发布于 2020-08-07 13:54:35
laravel 7的更新
您的语言/validation.php
es it/validation.php
'attributes' => [
'gallery.*.file' => 'Your custom message!!',
],发布于 2017-10-23 13:18:54
需要修改表单和控制器验证。
在形式上
{!! Form::open(['url' => 'actionURL']) !!}
{{ csrf_field() }}
<input name="gallery[]">
{!! Form::close() !!}在控制器中
foreach ($request->gallery as $key => $gallery) {
$validator = Validator::make(array('gallery => $gallery),
array('gallery' => 'required'));
}https://stackoverflow.com/questions/46889380
复制相似问题