Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

SmartyValidate with jQuery

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
iarfhlaith
Smarty Rookie


Joined: 20 Dec 2006
Posts: 13
Location: Dublin, Ireland

PostPosted: Tue Jan 15, 2008 1:52 pm    Post subject: SmartyValidate with jQuery Reply with quote

I'm trying to find an elegant solution to use the powerful SmartyValidate validation features and error notices with an AJAX call using jQuery.

I'm registering the validators when the page loads normally. And I'm also including the {validate} tags in the initial template. When I submit the form via jQuery's AJAX call I call a different script that attempts to connect to SmartyValidate and the instance that was registered in the initial page load.

I'm having two problems with this:

1. When reconnecting to SmartyValidate the registered validators have been forgotten.

2. Even if I get this to work, how can I get the {validate} tags to display in the initial page?

Any help is greatly appreciated. If I get it all working I'll post my code up so everyone can use it.

Many thanks,
Iarfhlaith Kelly
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Jan 15, 2008 2:28 pm    Post subject: Reply with quote

My guess is your client-side ajax call needs to pass along the session id (in the post?), then the server side needs to set that session to get to the smartyvalidate data.
Back to top
View user's profile Send private message Visit poster's website
iarfhlaith
Smarty Rookie


Joined: 20 Dec 2006
Posts: 13
Location: Dublin, Ireland

PostPosted: Tue Jan 15, 2008 2:36 pm    Post subject: Reply with quote

Thanks for the tip Monte,

I'll give it a go and get back to you.

Hopefully I'll have success with it. Using the structure of SmartyValidate with an AJAX call is something I'm sure a good few people would be interestred in. I'll post up my findings later today.

Regards,
Iarfhlaith
Back to top
View user's profile Send private message Send e-mail Visit poster's website
iarfhlaith
Smarty Rookie


Joined: 20 Dec 2006
Posts: 13
Location: Dublin, Ireland

PostPosted: Tue Jan 15, 2008 10:55 pm    Post subject: Reply with quote

OK, I've made good progress.

I can now validate the form variables registered in the initial page load using the jQuery AJAX call.

All I need to be able to do now is trigger the {validate} error messages to be displayed if there was an error.

I think I might have to write my own code for displaying these messages since the page doesn't do a reload.

Do you have any tips on what the best design pattern or coding practice would be to use here?

I'm delighted that I can use the validators, but it would be even better if I could use the error message feature as well.

My code is coming back to the JavaScript in JSON format. So for example if I entered an incorrect email address into a form with only one field that required an email address, the response from the AJAX call would be something like:

Code:

{'response':
    {
      'result':'failure'
     ,'text':'Please enter a valid email address'
    }
}


Is there anyway to get this response to trigger the {validate} tags in the original template?

I think I'm probably on my own from here on in. I've been scratching my head on this for absolutely ages and I don't think it's possible. I'll more than likely have to write some JavaScript to display the error messages
in line.

I'd love to come up with an elegant way to do this. Your ideas or comments are greatly appreciated.

Cheers,
Iarfhlaith
Back to top
View user's profile Send private message Send e-mail Visit poster's website
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Tue Jan 15, 2008 11:01 pm    Post subject: Reply with quote

You would have to write the error in JS, since the server side is already done processing the {validate} tags.
Back to top
View user's profile Send private message Visit poster's website
iarfhlaith
Smarty Rookie


Joined: 20 Dec 2006
Posts: 13
Location: Dublin, Ireland

PostPosted: Wed Jan 16, 2008 11:13 am    Post subject: Reply with quote

I've got everything sorted. I've come up with a pretty elegant way to support both synchronous and a-synchronous form submissions (IMHO).

Here's the idea:

    - Store all the error messages in an array in an include file and assign this to the page template.

    - Register SmartyValidate and all the validators in the initial page load.

    - Include the {validate} tags as per normal but use the error array to store the error text. Something like: message=$error.emailRequired.

    - When the form is submitted synchronously, process the form as per usual by connecting to SmartyValidate and using the is_valid() method.

    - When the form is submitted a-synchronously (to the same page), append something like ajax=true in the POST and then process the form normally again, but when submitting the output, display it in a different template that will render the result as JSON. Any error messages required can be retrieved from the array of error messages in the include file. The link to the error messages can be achieved by naming the index of the error message in the array with the same name given to that field in the form.

    - This method provides jQuery with everything it needs to render any required error messages on the fly while in AJAX mode.


Here are the major benefits of doing the form validation using this technique:

1. It supports both AJAX submissions and Non-AJAX submisions.

2. The error messaging is centralised through the usage of the error array.

3. The validation code is centralised (achieved through SmartyValidate for both AJAX and Non-AJAX)


Let me know what you think about this approach. I'll post up some example code when I have everything ironed out.

Any comments welcome.

Iarfhlaith
Back to top
View user's profile Send private message Send e-mail Visit poster's website
ZuqR
Smarty Rookie


Joined: 01 Dec 2007
Posts: 6

PostPosted: Mon Feb 18, 2008 2:09 pm    Post subject: Reply with quote

This sounds just like the thing I have been looking for. Could you please share some examples? Smile
Back to top
View user's profile Send private message Send e-mail
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> Add-ons All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP