emte
(Tesarm)
August 17, 2012, 11:11am
1
I’m using Yii user side validation in static forms and it’s great. But I don’t know how to add validators for ajax loaded elements.
I have simple form widget and I would like to load few more input fields into it via AJAX (that’s not problem with small jQuery script). But I don’t know how to add Yii javascript validators for loaded elements - I mean auto created JS validators like:
<script type="text/javascript">
/*<![CDATA[*/
jQuery(function($) {
$('#newsletter-form-footer').yiiactiveform({'validateOnSubmit':true,'validateOnChange':false,'afterValidate':Form.handleByAjax,'attributes':[{'id':'NewsletterForm_emailaddress','inputID':'NewsletterForm_emailaddress','errorID':'NewsletterForm_emailaddress_em_','model':'NewsletterForm','name':'emailaddress','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {
if($.trim(value)=='') {
messages.push(" VALIDATOR_REQUIRED");
}
if($.trim(value)!='' && !value.match(/^[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9-]*[a-zA-Z0-9])?$/)) {
messages.push(" VALIDATOR_EMAIL");
}
}}]});
});
/*]]>*/
</script>
Is there any way how to add or remove that validators "on the fly"?
Because if I add Validators here like this:
$email = new Yp_Form_Element(Yp_Form_Element::FIELD_textField, 'email', $this); $email->setRequired()
It creates javascript validators after page render is complete. But I need to add or remove some validators for new loaded elements also…
Haensel
(Johannes)
August 17, 2012, 11:41am
2
I don’t know if that’s an option but it might be easier to reload the whole form via ajax because CActiveForm will automatically render the js needed for the validators (don’t forget to set the fourth parameter of renderPartial to true to render js)
Anyways: If you need to get the js in your PHP code you could use the clientValidateAttribute() method of each validator. You get the validators via $model->validators and then use $validator->clientValidateAttribute to actually get the JS code that you could render yourself. This is just a rough idea but maybe it helps?
emte
(Tesarm)
August 17, 2012, 2:41pm
3
Haensel:
I don’t know if that’s an option but it might be easier to reload the whole form via ajax because CActiveForm will automatically render the js needed for the validators (don’t forget to set the fourth parameter of renderPartial to true to render js)
Anyways: If you need to get the js in your PHP code you could use the clientValidateAttribute() method of each validator. You get the validators via $model->validators and then use $validator->clientValidateAttribute to actually get the JS code that you could render yourself. This is just a rough idea but maybe it helps?
Great, thanks you a lot!
What I have now is following: I can get javascript code for every element - something like:
if($.trim(value)=='') {
messages.push("Nachname VALIDATOR_REQUIRED");
}
But I still don’t know, how to get other thinks like method definition:
/*<![CDATA[*/
jQuery(function($) {
$('#newsletter-form-footer').yiiactiveform({'validateOnSubmit':true,'validateOnChange':false,'afterValidate':Form.handleByAjax,'attributes':[{'id':'NewsletterForm_emailaddress','inputID':'NewsletterForm_emailaddress','errorID':'NewsletterForm_emailaddress_em_','model':'NewsletterForm','name':'emailaddress','enableAjaxValidation':false,'clientValidation':function(value, messages, attribute) {
//code above here
}}]});
});
/*]]>*/
How can I generate this?
I know this is an old post. Anyway i need to get whether you got the solution or not
emte
(Tesarm)
August 21, 2012, 7:37am
5
it’s not so old post I still don’t have solution for this. I don’t know how to generate rest of the javascript code… As I said in the previous reply
emte
(Tesarm)
August 21, 2012, 1:22pm
6
ok. it looks like it’s not problem to generate something like:
{
'id':'NewsletterForm_emailaddress',
'inputID':'NewsletterForm_emailaddress',
'errorID':'NewsletterForm_emailaddress_em_',
'model':'NewsletterForm',
'name':'emailaddress',
'enableAjaxValidation':false,
'clientValidation':function(value, messages, attribute) {
if($.trim(value)=='') {
messages.push("Nachname VALIDATOR_REQUIRED");
}
}}
you can $model->validators and then use $validator->clientValidateAttribute to generate
if($.trim(value)=='') {
messages.push("Nachname VALIDATOR_REQUIRED");
}
But!! How can I get informations above it? like id, name, model ??
After that I would be able to add or remove validators "on fly" really easy…
Thanks for answer…