Laravel 5.1,嵌套验证

时间:2015-09-14 15:00:29

标签: php validation 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',

更新编辑3:

添加新验证器:

    Validator::extend('false', function($attribute, $value, $parameters) {
        return false;
    });

并称之为          'cheese'=> '假',

什么都不做。 我想我在这里做了一些非常错误的事情,感谢任何帮助。

第二次更新到编辑3:

我认为问题是Form::checkbox如果没有选择则不会发送任何数据,因此不会触发验证。

false上拨打Form::text作为例外工作。

第三次更新到编辑3:

添加

{!! Form::hidden('cheese',0) !!}

复选框之前

使字段响应false验证程序。但required_if_then_required_without_all验证器仍未触发。

1 个答案:

答案 0 :(得分:0)

结构:

    O - Pizza option
    O - Free beer with pizza
    O - I want extras on my pizza
    ---- O - cheese
    ---- O - funghi
    ---- O - ham
    ---- O - something else 

验证

    Validator::extend('required_with_one_of', function($attribute, $value, $parameters, $validator)
    {
        $data = $validator->getData();
        $count = [];
        foreach ($parameters as $p) {
            if ( array_get($data,$p) != null) {$count[] = array_get($data, $p);}
        }
        if(count($count) > 0 ) { return true; } else { return false;}
    });

适用于extraOption喜欢

        'extraOption => 'required_with_one_of:options.cheese,options.funghi,options.ham,options.something_else',

复选框是:

    {!! Form::checkbox('extraOption', '1', false, ['id' => 'extraOption']) !!}

    <div class="form-group col-sm-12">
        {!! Form::label('options[cheese]','Cheese:', ['class' => 'padding-right-10']) !!}
        {!! Form::checkbox('options[cheese]', '1', false) !!}
    </div> ......
相关问题