Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 : Checkbox rules not working at signup for terms and conditions

Tags:

php

yii2

On signup, I need a certain message, but for some reason, it fails to show my message.

please help

I have the following rules:

['acordul_tc', 'required', 'on' => ['register']],
['acordul_tc', 'integer', 'max' => 1, 'message' => 'my test message'],

and the html:

<div style='position:relative; float:left; padding:5px;'>
<div style='float:left; padding:5px;'>
    <?php
        echo $form->field($model, 'acordul_tc', 
        ['options' => ['tag' => 'span',], 
        'template' => "{input}"])->checkbox(['checked' => false]);
    ?>
</div>
<div style='float:left; padding:5px;'>
    <?php
        echo \Yii::t('user', 'acordul_tc_start') . 
        Html::a(Yii::t('diverse', 'Termenii si Conditiile'), 
               ['site/terms-and-conditions']) . 
            \Yii::t('user', 'acordul_tc_end');
    ?>
</div>
</div>
<div class="col-lg-12">
<?php
    echo $form->field($model, 'acordul_tc', 
         ['template' => "{error}"])
         ->error();
?>

like image 705
Ionut Flavius Pogacian Avatar asked Nov 05 '14 13:11

Ionut Flavius Pogacian


2 Answers

If you want to show error message when checkbox is not checked, try this rule

 ['acordul_tc', 'required', 'on' => ['register'], 'requiredValue' => 1, 'message' => 'my test message']

This means that the attribute is required and must be equal to 1, else display error with your message.

like image 22
olchick Avatar answered Sep 22 '22 13:09

olchick


  1. The Accepted answer does not mark required Checkbox as 'RED' colored.
  2. It only shows the error message below the checkbox which is partially accepted, the solution must be acceptable only if it shows proper validation effects on UI and not just the message.
  3. Below is the code change need to be done on FORM to get expected output for End User.

    echo $form->field($model, 'acordul_tc', 
                    ['options' => ['tag' => 'span'], 
                    template' => "{input}"]
                )
              ->checkbox(['checked' => false, 'required' => true]);
    
  4. Below is the image that explains all.

enter image description here

like image 87
Ankur Soni Avatar answered Sep 25 '22 13:09

Ankur Soni