可以说我有一个这样的形式:Laravel 5.1,嵌套验证
(该O公司代表复选框,输入)
O - Pizza option O - Free beer with pizza O - I want extras on my pizza ---- O - cheese ---- O - funghi ---- O - ham ---- O - something else
用户可以选择比萨饼和啤酒免费,但也有一些附加功能。除了这些额外功能外,他还需要选择至少一种配料,但前提是选择了额外的配件。
我的验证如下:
'extras.cheese' => 'required_without_all:funghi,ham,something_else',
'extras.funghi' => 'required_without_all:cheese,ham,something_else',
'extras.ham' => 'required_without_all:cheese,funghi,something_else',
'extras.something_else' => 'required_without_all:cheese,funghi,ham',
不过,我想只有extraOption
选择了这些申请。换句话说,我想嵌套这些规则在required_if:extraOption,1
规则里面。
编辑:
我想我需要一个自定义的验证,这将是这个样子:
'extras.cheese' => 'required_if_then_required_without_all:extraOption,funghi,ham,something_else'
,其中第一选择会去功能required_if
一部分,其余选项required_without_all
部分。
我会在移动时编辑我的问题。
编辑2:
继文档(http://laravel.com/docs/5.1/validation#custom-validation-rules)我添加的代码下面app/Providers/AppServiceProvider.php
Validator::extend('required_if_then_required_without_all', function($attribute, $value, $parameters) {
return true;
});
现在我需要弄清楚什么是什么,以及如何实现的逻辑。
EDIT3:
这就是我认为应该工作,但事实并非如此。它总是通过验证。
Validator::extend('required_if_then_required_without_all', function($attribute, $value, $parameters) {
$required_if = $parameters[0];
$required_without_all = array_except($parameters, [0]); // remove entry with key 0 from $parameters array
$value = false;
if($required_if != 1)
{
$value = true;
}
else
{
foreach ($required_without_all as $r)
{
if ($r == 1) { $value = true; }
}
}
return $value;
});
我叫它为:
'cheese' => 'required_if_then_required_without_all:extraOption,funghi,ham,something_else',
UPDATE ON EDIT3:
添加新的验证器为:
Validator::extend('false', function($attribute, $value, $parameters) {
return false;
});
,把它作为 '奶酪'=> 'false',
什么都不做。 我想我在这里做的事情非常错误,任何帮助表示赞赏。
月2日更新TO EDIT3:
我认为问题是,如果不选择,以便验证没有被触发Form::checkbox
不发送任何数据。
调用false
对Form::text
作为例外。
3日更新到EDIT3:
复选框前添加
{!! Form::hidden('cheese',0) !!}
做出响应false
验证领域。但验证器required_if_then_required_without_all
仍然不会被触发。
如果有人有时间,请检查是否一切正常。这是工作,但我想知道是否可以改进。谢谢。 ps。我的代码并不那么简单,但我认为我根据这个简化的示例编辑了所有内容。 – dbr