gpt4 book ai didi

php - 使用 Laravel 5 中的输入数组验证本地化?

转载 作者:行者123 更新时间:2023-12-01 16:03:48 28 4
gpt4 key购买 nike

对于可以添加和修改字段的动态表单:

形式

<input name="gallery[1][title]">
<input name="gallery[1][text]">
.
.
.
<input name="gallery[n][title]">
<input name="gallery[n][text]">

在 Controller 中进行验证:
'gallery.*.file' => 'nullable|image',
'gallery.*.title' => 'nullable|string',

在本地化文件中:

我永远不知道数组中会有多少。
'gallery.*.text' =>  'text of gallery 1',
'gallery.*.title' => 'title of gallery 1',

我该怎么写?

我想要这样的结果:

title of gallery 1

.

.

.

title of gallery n

最佳答案

这是一个很酷的方法来做到这一点。不幸的是,laravel 目前不支持为特定 token 添加通用消息替换器,因此您可以执行以下操作:

在 Controller 中:

$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',

关于php - 使用 Laravel 5 中的输入数组验证本地化?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46889380/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com