|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
BobS Smarty Rookie
Joined: 27 Feb 2007 Posts: 6
|
Posted: Tue Feb 27, 2007 9:06 pm Post subject: SmartyValidate - dependent fields |
|
|
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 |
|
BobS Smarty Rookie
Joined: 27 Feb 2007 Posts: 6
|
Posted: Sat Mar 03, 2007 2:27 am Post subject: |
|
|
bump. Any ideas? |
|
Back to top |
|
BobS Smarty Rookie
Joined: 27 Feb 2007 Posts: 6
|
Posted: Tue Mar 06, 2007 7:21 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 06, 2007 8:25 pm Post subject: Re: SmartyValidate - dependent fields |
|
|
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 |
|
BobS Smarty Rookie
Joined: 27 Feb 2007 Posts: 6
|
Posted: Tue Mar 06, 2007 8:28 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 06, 2007 9:22 pm Post subject: |
|
|
All of the available formvars are passed into the validator as a parameter. |
|
Back to top |
|
BobS Smarty Rookie
Joined: 27 Feb 2007 Posts: 6
|
Posted: Tue Mar 06, 2007 9:25 pm Post subject: |
|
|
Understood, thank you. |
|
Back to top |
|
|
|
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
|