I am new to Yii.
I just want the client validation performed against some required fields:( user_name, password)
My view looks like this:
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'owner-confirm-form',
'enableAjaxValidation'=>true,
'[color="#8B0000"]enableClientValidation[/color]'=>true,
'focus'=>array($user,'user_name'),
'clientOptions' => array(
'validateOnSubmit'=>true,
'validateOnChange'=>true,
'validateOnType'=>false,
),
<p>
<span><?php echo Yii::t(owners_lang,'Pick a Username:');?><label
style="color: red;">*</label> </span>
<?php echo $form->textField($user,'user_name',array('size'=>25));?>
<?php echo $form->error($user, 'user_name'); ?>
</p>
<p>
<span><?php echo Yii::t(owners_lang,'Enter your password:');?><label
style="color: red;">*</label> </span>
<?php echo $form->passwordField($user,'password',array('size'=>25));?>
<?php echo $form->error($user, 'password'); ?>
</p>
<?php $this->endWidget(); ?>
In Users model,
just simple rules like this:
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('user_name,password,repeat_password,name', 'required'),
)
I didnt put controller code as its irrelevant for client side validation.
I want the client validation to be performed when user finish typing and press tab to shift focus.
I can see yiiactiveform.js is loaded,and the client validation javascript is injected into the source code
However,when I debug into yiiactiveform,
$.each(settings.attributes, function(i, attribute) {
if (attribute.validateOnChange) {
$('#'+attribute.inputID, $form).change(function(){
var inputType = $('#'+attribute.inputID).attr('type');
validate(attribute, inputType=='checkbox' || inputType=='radio');
}).blur(function(){
[color="#8B0000"] if(attribute.status!=2 && attribute.status!=3)[/color]
validate(attribute, !attribute.status);
});
}
if (attribute.validateOnType) {
$('#'+attribute.inputID, $form).keyup(function(){
if (attribute.value != $('#'+attribute.inputID, $form).val())
validate(attribute, false);
});
}
});
the attribute.status = 1, thus when call into validate: the forcevalidate is false, and since
this.value == $(’#’+this.inputID, $form).val() (I didnt type anything, and just press tab, hoping the required validation should work showing this field is required), it will never go to the real validation work.
what I am still missing here?
var validate = function(attribute, forceValidate) {
if (forceValidate)
attribute.status = 2;
$.each(settings.attributes, function(){
if (this.value != $('#'+this.inputID, $form).val()) {
this.status = 2;
forceValidate = true;
}
});
if (!forceValidate)
return;
if(settings.timer!=undefined) {
clearTimeout(settings.timer);
}
Thanks much!