Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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],
        ];
    }
Posted
Comments
ZurdoDev 19-Feb-18 9:35am    
Validation for checkboxes really does not mean anything. So, my guess is the validation engine you are using ignores checkboxes.

Just write some code yourself to make sure the checkbox is checked. Why bother putting a checkbox on a form if the user HAS to check it? It usually does not make any sense.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900