【问题标题】:Laravel 4.2 required_if validation with a checkbox arrayLaravel 4.2 required_if 使用复选框数组进行验证
【发布时间】:2015-01-16 01:48:30
【问题描述】:

我不知道如何让 required_if 验证规则起作用。它似乎没有运行,但可能是因为我没有正确使用它。所有其他规则都运行,但这条规则似乎不起作用。谁能告诉我我做错了什么?

这是 HTML(带有 Blade)

     <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6">
    {{ Form::checkbox('class_of_mail[]', 'Nonprofit Standard (Bulk)', null, ['data-id' => 'non-profit-standard-bulk']) }} Nonprofit Standard (Bulk)* <br />
    {{ Form::checkbox('class_of_mail[]', 'Presorted Standard (Bulk)', null, ['data-id' => 'presorted-standard-bulk']) }} Presorted Standard (Bulk)* <br />
    {{ Form::checkbox('class_of_mail[]', 'Presorted First Class (500 pieces or more)', null, ['data-id' => 'presorted-first-class']) }} Presorted First Class (500 pieces or more) <br />
    {{ Form::checkbox('class_of_mail[]', 'First Class', null, ['data-id' => 'first-class']) }} First Class <br />
    {{ Form::checkbox('class_of_mail[]', 'Campus', null, ['data-id' => 'campus']) }} Campus <br />
    {{ Form::checkbox('class_of_mail[]', 'Other',  null, ['data-id' => 'class_of_mail_other']) }} Other <br />
    {{ Form::checkbox('class_of_mail[]', 'Customer Provided List', null, ['data-id' => 'class_of_mail_customer_provided_list']) }} I would like to provide my own campus list <br />
    </div>

    <div id="mailing_class_other_div" class="form-group row hide">
        {{ Form::label('mailing_class_other', 'Other Class of Mail', ['class' => 'control-label col-lg-12 col-md-12 col-sm-12 col-xs-12']) }}
        <div class="col-lg-5 col-md-5 col-sm-5 col-xs-5">
        {{ Form::text('mailing_class_other', null, ['class' => 'form-control input-sm']) }}
        </div>
    </div> 

这是我的控制器中的 $rules 数组。此表单没有模型。

    $rules = [
        'department' => 'required|min:2|max:64',
        'purchase_requisition' => 'required|min:2|max:64',
        'contact_name' => 'required|min:2|max:64',
        'contact_phone' => 'required|min:2|max:32',
        'alt_contact_name' => 'required_with:alt_contact_phone',
        'alt_contact_phone' => 'required_with:alt_contact_name',
        'mailing_subject' => 'required|min:2|max:64',
        'mailing_piece_count' => 'required|min:1|max:11',
        'class_of_mail' => 'required',
        ## Here is the required_if validation rule ##
        'mailing_class_other' => 'required_if:class_of_mail,Other',
    ];

    $validator = Validator::make($input, $rules, $messages);

    if ($validator->fails())
    {
        $messages = $validator->messages();
        return Redirect::to('print-to-mail')->withErrors($validator)->withInput();
    }

【问题讨论】:

  • 编辑为 mailing_class_other 字段添加 HTML

标签: php laravel laravel-4 laravel-validation


【解决方案1】:

看起来您的所有字段都在 'class_of_mail' 数组中发送,所以 $POST 数组看起来像:

array(
    'class_of_mail' => array('Other'),
    'mailing_class_other' => '...'
)

您对mailing_class_other 的验证只有在'class_of_mail' == 'Other' 时才会生效,而'class_of_mail' 实际等于array('Other')

我不认为 Laravel 有办法用数组值处理这个规则,所以我认为你最好的办法是有条件地添加规则,就像在文档的这一部分:http://laravel.com/docs/4.2/validation#conditionally-adding-rules

$validator->sometimes('mailing_class_other', 'required', function($input)
{
    return in_array($input->class_of_mail, 'Other');
});

如果'class_of_mail' 数组包含'Other' 的值,这将使'mailing_class_other' 字段成为必需字段。

【讨论】:

    猜你喜欢
    • 2012-12-08
    • 2016-10-13
    • 2017-04-17
    • 2018-08-27
    • 2021-03-06
    • 2022-12-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    相关资源
    最近更新 更多