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

Multiple validator for a single form field

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


Joined: 17 Apr 2007
Posts: 3

PostPosted: Tue Apr 17, 2007 5:36 pm    Post subject: Multiple validator for a single form field Reply with quote

I am strugling with multiple validators for a same form field which results in one error message at a time when the form field does not matches the validator rules.

Let me show the case briefly:

Smarty template file:
Code:
<INPUT NAME="field1" TYPE="TEXT" />
{validate id="rule_1" message="Field can not be empty"}
{validate id="rule_2" message="Field is not a number"}
...template implements other form input fields and validator...


PHP code fragment:
Code:
SmartyValidate::connect($smarty, true);
// syntax: SmartyValidate::register_validator($validator id, $formname, $validator, $empty, $halt, $transform)
SmartyValidate::register_validator('rule_1', 'field1', 'notEmpty', false, false, 'trim');
SmartyValidate::register_validator('rule_2', 'field1', 'isInt', true, false, 'trim');
SmartyValidate::register_validator('another_field', 'another_field', 'isEmpty', false, false, 'trim');


Desired result:
1. If field is empty: output message: 'Field can not be empty' (do not output message 2)
2. If field is not a number: output message 'Field is not a number'
3. Other validator output...

If the form is posted completely unfilled then:
rule 1 fails due to $empty = false
rule 2 succeeds even though field1 isn't numerical, due to $empty = true

Remember I do want to show two different validation message, so only use rule 2 with $empty = false is not an option in my case.

Actually result:
empty form field posted: shows validation message 1 + 2 (UNCORRECT, WHY?)
non-numerical form field posted: shows validation message 2(CORRECT)
numerical form field posted: no validation message occures (CORRECT)

Can anyone explain where it goes wrong?

Other point of interest:
How do you manage multiple validation error messages for a single form field in your smarty templates. Assign all the output to smarty vars and show only the significant message using a lot of {if} {else} branches ?

Looking forward for some hints about this.

Kind regards,
Broken Frame
Back to top
View user's profile Send private message
BrokenFrame
Smarty n00b


Joined: 17 Apr 2007
Posts: 3

PostPosted: Tue Apr 17, 2007 6:10 pm    Post subject: Reply with quote

Oops, found posts strongly related to this topic:

http://www.phpinsider.com/smarty-forum/viewtopic.php?t=8744
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5505

As suggest in these posts, some code was tweaked. Wondered if there is a CVS for SmartyValidate or is code only maintained by Morth?

Regards,
Broken Frame

P.S. If there is an update on this subject however, feel free for post it here.
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