chux
(Djchux)
August 22, 2011, 3:35pm
1
Hi,
I have a contact form, and im using client validation
$form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableClientValidation'=>true,
'enableAjaxValidation' => false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'focus' => array($model, 'nombre'),
));
and in my required checkbox
echo $form->checkBox($model, 'condiciones');
echo $form->labelEx($model, 'condiciones', array(), array('uncheckValue' => ''));
echo $form->error($model, 'condiciones');
in my form model rules:
array('condiciones', 'required', 'requiredValue' => 1, 'message' => 'Tienes que leer y aceptar nuestra política de datos'),
If i put enableClientValidation to false it’s working ok, but with client validation, if a check and then uncheck, the validation pases.
any help would be appreciated.
jowen
(Ljowen985)
August 23, 2011, 2:04am
2
hi,
rule:
array(‘condiciones’, ‘boolean’, ‘falseValue’ => ‘true’),
then set
‘enableAjaxValidation’ => true,
work well for me. =)
chux
(Djchux)
August 23, 2011, 6:12am
3
FalseValue? i guess you mean requiredValue. Anyway, it doesn’t work.
a) it only validates de checkbox the first time (all the others fields are working / validating ok)
i don’t want ajax validation. I only want client validation.
Thanks
chux
(Djchux)
August 23, 2011, 2:42pm
4
For example, this code
echo $form->labelEx($model, 'condiciones',array('uncheckValue' => '0'));
echo $form->error($model, 'condiciones', array('clientValidation' => '
console.log(value);
'));
So, when i click in the check i will see the value in the console. Allways (checked and unchecked) displays 1. So the client validation, is always passing
Lokhus
(Anderaznar)
March 21, 2012, 1:13pm
5
I wasn’t able to make the client side validation work using the method you explained so I ended up setting AJAX validation to false and setting up the rule for the checkbox like this:
array('conditions', 'compare', 'compareValue' => 1, 'message'=>'You have to check this checkbox'),
And then I used this little piece of JQuery:
$("input[type='checkbox']").each(function(){
if(!$(this).is(':checked')) $(this).val("0");
$(this).click(function(){
if($(this).is(':checked')) {
$(this).val("1");
}
else {
$(this).val("0");
}
});
});