【问题标题】:Laravel 5.1, nested validationLaravel 5.1,嵌套验证
【发布时间】:2015-09-14 15:00:28
【问题描述】:

假设我有这样的表格:

(O代表复选框输入)

O - 比萨选项 O - 免费啤酒和比萨饼 O - 我想在我的披萨上加点东西 ---- O - 奶酪 ---- O-funghi ---- O-火腿 ---- O - 别的东西

用户可以选择披萨和免费啤酒,还可以选择一些额外的东西。在这些额外选项中,他需要至少选择一种成分,但前提是选择了额外选项。

我的验证如下:

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

EDIT3 更新:

添加新的验证器为:

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

并将其称为 '奶酪' => '假',

什么都不做。 我想我在这里做错了什么,感谢任何帮助。

EDIT3 的第二次更新:

我认为问题在于Form::checkbox 如果未选中则不会发送任何数据,因此不会触发验证。

在Form::text 上调用false 可以例外。

第三次更新到 EDIT3:

添加

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

在复选框使字段响应false Validator 之前。但是required_if_then_required_without_all 验证器仍然没有被触发。

【问题讨论】:

    标签: php validation laravel-5.1


    【解决方案1】:

    结构:

        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;}
        });
    

    适用于extraOptionlike

            '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> ......
    

    【讨论】:

    • 如果有人有时间,请检查是否一切正常。这是有效的,但我想知道是否可以改进。谢谢。附言。我的代码不是那么简单,但我想我根据这个简化的例子编辑了所有内容。
    猜你喜欢
    • 1970-01-01
    • 2018-08-15
    • 2016-03-27
    • 1970-01-01
    • 2019-04-15
    • 2015-10-09
    • 2018-10-29
    • 2016-02-25
    • 2016-06-11
    相关资源
    最近更新 更多