Click here to Skip to main content
13,666,418 members
Rate this:
 
Please Sign up or sign in to vote.
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 19-Feb-18 2:24am
Comments
‭011111100010‬ 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180810.1 | Last Updated 19 Feb 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100