如果有几个问题显示带有排名的选择ID(选择第一个复选框为首选,等等)。这是通过以下形式发送的简化的var_dump($_POST)
:
Array
(
[_token] => 6vPgIM1MX7MJ0DzMX2OFjCdPEBBel5mRJ6RzrEjR
[11] => Array // Means question id 11
(
[42] => 2 // Means choice id 42 was chosen second
[43] => 1 // Means choice id 43 was chosen first
[44] => 3 // Means choice id 44 was chosen third
)
[12] =>
[13] => Array
(
[4] => 1
)
[btn-validate] => CONTINUE
)
然后我的var_dump($validationRules)
如下:
Array
(
[11] => Array
(
[0] => between:0,2
)
[12] => Array
(
[0] => between:1,3
)
[13] => Array
(
[0] => between:0,1
)
)
最后是var_dump($validationMessages)
(但在这里没有那么重要):
Array
(
[11.between] => You should select between 0 and 2 choices
[12.between] => You should select between 1 and 3 choices
[13.between] => You should select between 0 and 1 choices
)
这都是这样包裹的:
$validator = Validator::make($request->all(), $validationRules, $validationMessages);
if ($validator->fails()) {
var_dump($validator->errors());
exit('fail'); // Never ends up here
}
exit('all good'); // Always ends up here
如何使between:x,y
验证规则与值数组一起使用?
答案 0 :(得分:0)
我认为您误解了数组之间的规则是如何工作的。
(...)字符串,数字,数组和文件的计算方式与大小规则相同。
(...)对于数组,大小对应于数组的计数。 (...)
意味着您指定的验证规则意味着将评估每个数组值的计数。在每种情况下,由于每种情况下的计数均为1,这些值均被评估为true。
答案 1 :(得分:0)
将此作为您的复选框 //刀片文件
<div class="form-group">
<label for="likes_turtles" class="col-md-3 control-label">Likes Turtles</label>
<div class="col-md-6">
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="Yes"> Yes
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="No"> No
</label>
</div>
<div class="checkbox">
<label>
<input type="checkbox" name="likes_turtles[]" value="Maybe so"> Maybe so
</label>
</div>
</div>
</div>
然后您就可以在控制器中进行验证,例如下面的代码
[
"likes_turtles" => "required",
"likes_turtles.*" => "in:Yes,No,Maybe so"
]
答案 2 :(得分:0)
找出问题所在。
我所有的规则都还可以,问题是Laravel的验证系统不支持将整数用作$_POST
键
Laravel问题: https://github.com/laravel/framework/issues/29116