chasxu
(Liwei Xu)
December 22, 2011, 11:39am
1
I got an problem in the ajax validation on a hidden field ‘license_number’ on CActiveForm. (The ajax validation on all the other form fields are working.)
license_number (hidden) = license_number1+license_number2, e.g. ABC123 = ABC + 123.
There is a javascript function to update license_number when license_number1 or license_number2 is changed. The problem is the ajax validation on the license_number never triggers.
Please see the code snippet below:
View:
<div id="fieldLicenseNumber">
<span class="label"><?php echo $form->label($model, $attribute="license_number", array('required' => true));?></span>
<!--license_number=license_number1 + license_number2 e.g. ABC123 = ABC + 123-->
<span class="input" id="license_number"><?php echo $form->hiddenField ($model, $attribute="license_number");?></span>
<span class="input" id="license_number1"><?php echo $form->textField ($model, $attribute="license_number1");?></span>
<span> - </span>
<span class="input" id="license_number2"><?php echo $form->textField ($model, $attribute="license_number2");?></span>
<span class="error"><?php echo $form->error($model, $attribute="license_number");?></span>
</div>
Model:
//validation rule
array('license_number','validateLicenseRequired'),
......
function validateLicenseRequired($attribute, $params) {
$isLicenseEmpty = empty($this->license_number);
if ($isLicenseEmpty) {
$this->addError(
$attribute,
$error=Yii::t('validation', 'required', array('{fieldName}'=> $this->getAttributeLabel('license_number')) )
);
}
}
Thanks in advance.
jayant
(Codesutras)
December 22, 2011, 12:01pm
2
I got an problem in the ajax validation on a hidden field ‘license_number’ on CActiveForm. (The ajax validation on all the other form fields are working.)
license_number (hidden) = license_number1+license_number2, e.g. ABC123 = ABC + 123.
There is a javascript function to update license_number when license_number1 or license_number2 is changed. The problem is the ajax validation on the license_number never triggers.
Please see the code snippet below:
View:
<div id="fieldLicenseNumber">
<span class="label"><?php echo $form->label($model, $attribute="license_number", array('required' => true));?></span>
<!--license_number=license_number1 + license_number2 e.g. ABC123 = ABC + 123-->
<span class="input" id="license_number"><?php echo $form->hiddenField ($model, $attribute="license_number");?></span>
<span class="input" id="license_number1"><?php echo $form->textField ($model, $attribute="license_number1");?></span>
<span> - </span>
<span class="input" id="license_number2"><?php echo $form->textField ($model, $attribute="license_number2");?></span>
<span class="error"><?php echo $form->error($model, $attribute="license_number");?></span>
</div>
Model:
//validation rule
array('license_number','validateLicenseRequired'),
......
function validateLicenseRequired($attribute, $params) {
$isLicenseEmpty = empty($this->license_number);
if ($isLicenseEmpty) {
$this->addError(
$attribute,
$error=Yii::t('validation', 'required', array('{fieldName}'=> $this->getAttributeLabel('license_number')) )
);
}
}
Thanks in advance.
Update this line with
<span class="error"><?php echo $form->error($model, $attribute="license_number");?></span>
<span class="error"><?php echo $form->error($model,"license_number");?></span>
then check it…might be this will work.
if not then let me know…
mdomba
(Maurizio Domba Cerin)
December 22, 2011, 12:23pm
3
If your logic is that the license_number is always the combination of two other fields… why are you creating the hidden field and validating this one…
You just need to validate the other two fields to be required and proper… than in the backend you just calculate the license_number.
jayant
(Codesutras)
December 22, 2011, 12:31pm
4
mdomba:
If your logic is that the license_number is always the combination of two other fields… why are you creating the hidden field and validating this one…
You just need to validate the other two fields to be required and proper… than in the backend you just calculate the license_number.
I do agree with you.but if we want to calculate it in the client side then…i guess we should have to validate it like a normal model rule…
However it is totally depends on the logical approach.
chasxu
(Liwei Xu)
December 22, 2011, 1:15pm
5
@Jayant :
remove the ‘attribute’ in the error span but no luck.
@mdomba :
I tried validating the license_number1 and license_number2. But it still does not work. Please see the code snippet as below:
//in the model
array('license_number1, license_number2','validateLicenseRequired'),
function validateLicenseRequired($attribute, $params) {
$isLicenseEmpty = empty($this->$attribute);
if ($isLicenseEmpty) {
$this->addError(
$attribute='license_number',
$error=Yii::t('validation', 'required', array('{fieldName}'=> $this->getAttributeLabel('license_number')))
);
}
The error is in the code above. It does not even run up to this function. The problem is the ajax validation on these two fields just does not trigger.
mdomba
(Maurizio Domba Cerin)
December 22, 2011, 1:39pm
6
If you go with my idea… then just use the standard ‘reqired’ validator like:
array('license_number1, license_number2','required')
chasxu
(Liwei Xu)
December 22, 2011, 1:45pm
7
mdomba:
If you go with my idea… then just use the standard ‘reqired’ validator like:
array('license_number1, license_number2','required')
I used the user-defined validation function because
There is only one error span
<span class="error"><?php echo $form->error($model, "license_number");?></span>
and no error span for license_number1 and license_number2
there are some other conditions to check in the user-defined validation function.
Do not know why the processAjaxValidation function is not triggered when the cursor leaves the license textfields …
mdomba
(Maurizio Domba Cerin)
December 22, 2011, 2:12pm
8
Ddi you remove the "attribute=" like jayant suggested… but from all the calls, not only on the error call ?
chasxu
(Liwei Xu)
December 22, 2011, 2:16pm
9
Yes, I tried removing all the ‘attribute=’.
BTW, if I submit the form, the validation runs correctly.
It seems both license_number1 and license_number2 must have related errors fields for the ajax validation to work. After I add two error span for these two fields, the ajax validation is triggered. But it will add two duplicate error messages onto the row and there is some weird issue when both fields are not empty, the error message ‘required’ is still there.