Mar*_*ria 5 php localization laravel-5 laravel-validation laravel-localization
对于可以添加和修改字段的动态表单:
通知
<input name="gallery[1][title]">
<input name="gallery[1][text]">
.
.
.
<input name="gallery[n][title]">
<input name="gallery[n][text]">
Run Code Online (Sandbox Code Playgroud)
在控制器中进行验证:
'gallery.*.file' => 'nullable|image',
'gallery.*.title' => 'nullable|string',
Run Code Online (Sandbox Code Playgroud)
在本地化文件中:
我永远不知道阵列中会有多少个。
'gallery.*.text' => 'text of gallery 1',
'gallery.*.title' => 'title of gallery 1',
Run Code Online (Sandbox Code Playgroud)
我该怎么写呢?
我想要这样的结果:
画廊名称 1
。
。
。
画廊名称 n
这里有一个很巧妙的方法来做到这一点。不幸的是,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
}
Run Code Online (Sandbox Code Playgroud)
您还可以在服务提供商中添加替换器,以使它们在所有路由中可用,但不幸的是,您需要为您希望它们可用的每个规则注册它们。
在本地化文件中:
'gallery.*.text' => 'text of gallery :index',
'gallery.*.title' => 'title of gallery :index',
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1641 次 |
| 最近记录: |