Yii CActiveForm date validation

2020-06-29 22:49发布

问题:

The date validation for the "Text Date" field isn't working.

http://sky-walker.net/temp/test/yii/testdate/index.php?r=site/login

The "required" validation works if I set it as required though.

What I want the text field to do is to either turn red or green when the form is submitted and also when the focus is lost using ajax. If it turns red an error message should also appear.

From login.php

<div class="row">
    <?php echo $form->labelEx($model,'textDate'); ?>
    <?php echo $form->textField($model,'textDate'); ?>
    <?php echo $form->error($model,'textDate'); ?>
</div>

From LoginForm.php

public $textDate;

From rules()

...array('textDate', 'date', 'format'=>'d/M/yyyy'),...

回答1:

On your form you do not have ajax validation. The validation you have is javascript validation. If you take a look with Firebug then you will see nothing is communicated with the server => you only have js validation, you do not validate the model.

YOu have to set up some variables to make it run through ajax

'enableAjaxValidation' => true,
'clientOptions' => array(
        'validateOnSubmit' => true,
        'validateOnChange' => true,
),