未选中的复选框不会在yii2中传递所需的验证 [英] Unchecked checkbox don't pass required validation in yii2
本文介绍了未选中的复选框不会在yii2中传递所需的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试验证表单中的输入,除了复选框外,所有输入都有效。我做错了什么?
< div class =transactions-form>
<?php $ form = ActiveForm :: begin(); ?>
<?= $ form->字段($ model,'who_paid_id') - > dropDownList($ users) - >标签('谁支付')?>
<?= $ form->字段($ model,'service_id') - > dropDownList($ services) - > label('Service')?>
< input type =hiddenid =transactions-who_paidname =Transactions [who_paid]>
< input type =hiddenid =transactions-servicename =Transactions [service]>
< input type =hiddenid =transactions-amount_per_personname =Transactions [amount_per_person]>
<?= $ form->字段($ model,'amount') - > textInput()?>
< div id =transactions-who_has_to_pay_for_it_idsinline =aria-required =truearia-invalid =true>
<?php foreach($ users as $ key => $ value){?>
< label>< input type =checkboxname =Transactions [who_has_to_pay_for_it_ids] [<?= $ key?>]value =<?= $ key?> <?php if(in_array($ key,$ whoHasToPay)){echo'CHECKED'; }>> <?= $ value?>< / label>
<?php}?>
< label>< input type =checkboxname =Transactions [who_has_to_pay_for_it_ids] [0]value =0>大家< /标签>< / DIV>
<?= $ form->字段($ model,'date_of_payment') - > textInput(array('type'=>'date'))?>
< div class =form-group>
<?= Html :: submitButton($ model-> isNewRecord?'Create':'Update',['class'=> $ model-> isNewRecord?'btn btn-success':' btn btn-primary'])?>
< / div>
<?php ActiveForm :: end(); ?>
< / div>
我的尝试:
公共职能规则()
{
return [
[['who_paid_id','service_id', 'amount','who_has_to_pay_for_it_ids','date_of_payment'],'required'],
[['who_paid_id','service_id'],'整数'],
[['金额','amount_per_person '],'数字'],
[['who_has_to_pay_for_it'],'字符串'],
[['date_of_payment','creation_date'],'安全'],
[[ 'who_paid','service'],'string','max'=> 45],
];
}
解决方案
form = ActiveForm :: begin(); ?>
<?=
form-> field(
model,'who_paid_id') - > dropDownList(
I have tried to validate inputs in my form all of them works, except checkbox. What am i doing wrong?
<div class="transactions-form"> <?php $form = ActiveForm::begin(); ?> <?= $form->field($model, 'who_paid_id')->dropDownList($users)->label('Who paid') ?> <?= $form->field($model, 'service_id')->dropDownList($services)->label('Service') ?> <input type="hidden" id="transactions-who_paid" name="Transactions[who_paid]" > <input type="hidden" id="transactions-service" name="Transactions[service]" > <input type="hidden" id="transactions-amount_per_person" name="Transactions[amount_per_person]" > <?= $form->field($model, 'amount')->textInput() ?> <div id="transactions-who_has_to_pay_for_it_ids" inline="" aria-required="true" aria-invalid="true"> <?php foreach ($users as $key=>$value){ ?> <label><input type="checkbox" name="Transactions[who_has_to_pay_for_it_ids][<?= $key ?>]" value="<?= $key ?>" <?php if(in_array($key, $whoHasToPay)){ echo 'CHECKED'; } ?>> <?= $value ?></label> <?php } ?> <label><input type="checkbox" name="Transactions[who_has_to_pay_for_it_ids][0]" value="0"> Everybody</label></div> <?= $form->field($model, 'date_of_payment')->textInput(array('type'=>'date')) ?> <div class="form-group"> <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?> </div> <?php ActiveForm::end(); ?> </div>
What I have tried:
public function rules() { return [ [['who_paid_id', 'service_id', 'amount', 'who_has_to_pay_for_it_ids', 'date_of_payment'], 'required'], [['who_paid_id', 'service_id'], 'integer'], [['amount', 'amount_per_person'], 'number'], [['who_has_to_pay_for_it'], 'string'], [['date_of_payment', 'creation_date'], 'safe'], [['who_paid', 'service'], 'string', 'max' => 45], ]; }
解决方案
form = ActiveForm::begin(); ?> <?=
form->field(
model, 'who_paid_id')->dropDownList(
这篇关于未选中的复选框不会在yii2中传递所需的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文