Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

SmartyValidate - dependent fields

 
Post new topic   Reply to topic    Smarty Forum Index -> Add-ons
View previous topic :: View next topic  
Author Message
BobS
Smarty Rookie


Joined: 27 Feb 2007
Posts: 6

PostPosted: Tue Feb 27, 2007 9:06 pm    Post subject: SmartyValidate - dependent fields Reply with quote

I'm creating a form that requires the user's State and Country. US and Canadian visitors will supply their state or province, whereas this is not necessary for other countries.

I cannot find a way to make the validation work so that if the country dropdown is US or Canada, then it will require a state to be selected in the state dropdown. If it is not one of these countries then the field is not required.

I didn't see a mechanism through which you could 'throw' an error, since that would seem to solve the problem.

Any ideas? Thanks in advance.
Back to top
View user's profile Send private message
BobS
Smarty Rookie


Joined: 27 Feb 2007
Posts: 6

PostPosted: Sat Mar 03, 2007 2:27 am    Post subject: Reply with quote

bump. Any ideas?
Back to top
View user's profile Send private message
BobS
Smarty Rookie


Joined: 27 Feb 2007
Posts: 6

PostPosted: Tue Mar 06, 2007 7:21 pm    Post subject: Reply with quote

No response, so I figured out a workaround in the meantime. I broke the form up into two validation forms, default and StateID. The validator for StateID is dummyNotValid, a new validator I added. It returns false at all times. I run the validation on the default form first. Then, I do checking to see if the country is US and the state is not selected. If so, the state is invalid, so I validate the StateID form, which always returns false and throws the error I need. If the default form is good and the state ID is properly configured, I don't 'validate' the form. In this manner I can throw an error whenever I decide, via code logic, that an error is warranted. This gets me around the limitations of the SmartyValidate class.

Comments/suggestions/improvements welcome.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 06, 2007 8:25 pm    Post subject: Re: SmartyValidate - dependent fields Reply with quote

BobS wrote:
I'm creating a form that requires the user's State and Country. US and Canadian visitors will supply their state or province, whereas this is not necessary for other countries.

I cannot find a way to make the validation work so that if the country dropdown is US or Canada, then it will require a state to be selected in the state dropdown. If it is not one of these countries then the field is not required.

I didn't see a mechanism through which you could 'throw' an error, since that would seem to solve the problem.

Any ideas? Thanks in advance.


If you are using SmartyValidate, this should be as simple as creating a custom validator for the state field, and make it dependent on the country value. The validator has access to all of the form variables.
Back to top
View user's profile Send private message Visit poster's website
BobS
Smarty Rookie


Joined: 27 Feb 2007
Posts: 6

PostPosted: Tue Mar 06, 2007 8:28 pm    Post subject: Reply with quote

That makes sense. This wasn't clear from looking at the documentation. I didn't see any way to make the custom validator aware of the value of another field on the form. Can you point me to a specific example? Thanks.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Mar 06, 2007 9:22 pm    Post subject: Reply with quote

All of the available formvars are passed into the validator as a parameter.
Back to top
View user's profile Send private message Visit poster's website
BobS
Smarty Rookie


Joined: 27 Feb 2007
Posts: 6

PostPosted: Tue Mar 06, 2007 9:25 pm    Post subject: Reply with quote

Understood, thank you.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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