[英]Laravel 4.2 required_if validation with a checkbox array
I can't figure out how to get the required_if validation rule to work. 我不知道如何使required_if验证规则起作用。 It doesn't seem to run but probably because I am not using it correctly.
它似乎没有运行,但可能是因为我没有正确使用它。 All the other rules run but this rule doesn't seem to work.
所有其他规则都运行,但是该规则似乎不起作用。 Can somebody tell me what I am doing wrong?
有人可以告诉我我做错了吗?
Here is the HTML (with Blade) 这是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>
Here is the $rules array in my controller. 这是我控制器中的$ rules数组。 There is no model for this form.
此表格没有模型。
$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();
}
It looks like all of your fields are being sent across in the the 'class_of_mail'
array, so the $POST
array will look something like: 您的所有字段似乎都在
'class_of_mail'
数组中发送,因此$POST
数组将类似于:
array(
'class_of_mail' => array('Other'),
'mailing_class_other' => '...'
)
Your validation for the mailing_class_other
only kicks in if 'class_of_mail' == 'Other'
, which it doesn't, 'class_of_mail'
actual equals array('Other')
. 仅当
'class_of_mail' == 'Other'
,才开始对mailing_class_other
验证,而'class_of_mail'
实际上等于array('Other')
。
I don't think Laravel has a way of handling this rule with array values, so I think your best bet is to conditionally add the rule like in this section of the documention: http://laravel.com/docs/4.2/validation#conditionally-adding-rules 我不认为Laravel有使用数组值处理此规则的方法,所以我认为最好的选择是像在本文档的此部分中一样有条件地添加该规则: http : //laravel.com/docs/4.2/validation #有条件地加入规则
$validator->sometimes('mailing_class_other', 'required', function($input)
{
return in_array($input->class_of_mail, 'Other');
});
This will make the 'mailing_class_other'
field required if the 'class_of_mail'
array contains a value of 'Other'
. 如果
'class_of_mail'
数组包含'Other'
值, 'class_of_mail'
需要'mailing_class_other'
字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.