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

SmartyValidate: second validation skipped and returns error

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


Joined: 24 Aug 2006
Posts: 3

PostPosted: Thu Aug 24, 2006 11:42 pm    Post subject: SmartyValidate: second validation skipped and returns error Reply with quote

Okay, I need a little help here. I have a date that is first checked for a valid date, then checked for after another date. If the date is not valid, the second check is always returned as an error.

Here's my PHP registration:
Code:

      $validator->register_validator('endDateIsValidDate', 'endDate', 'isValidDate', FALSE, FALSE, 'makeDate');
      $validator->register_validator('endDateIsDateAfter', 'endDate:beginDate', 'isDateAfter', FALSE, FALSE, 'makeDate');


Here's my template:
Code:

    {validate page="1" id="endDateIsValidDate" message="<font color='red'>End Date must be a valid date.</font><br/>"}
    {validate page="1" id="endDateIsDateAfter" message="<font color='red'>End Date must be after Begin Date.</font><br/>"}


I've traced it in the SmartyValidate class to the section with the comment
Code:

// already failed, skip this test


It appears that because endDate is already in the $_ret array, the second validation is skipped, never setting the ['valid'] entry in the ['validators'] array. This appearantly is interpretted later as "invalid".

I was able to get everything to work by commenting out that section of the class code, but I'm sure that's not the right answer. I'm obviously doing something wrong.

Any help would be appreciated.
Back to top
View user's profile Send private message
mckneisler
Smarty n00b


Joined: 24 Aug 2006
Posts: 3

PostPosted: Fri Aug 25, 2006 8:19 pm    Post subject: Reply with quote

I believe that this is the same issue as the Topic started here:
http://www.phpinsider.com/smarty-forum/viewtopic.php?t=5505

My solution right now was to modify the class code within the "_failed_fields" function as such:
Code:

            if(is_array($_ret) && in_array($_full_field, $_ret)) {
                // already failed, skip this test
                $_sess[$_key]['valid'] = 1;  // added this line
                continue;   
            }


What this does is skips further validation on the field in error without posting messages from subsequent validators.

I think the true proper fix should allow for a parameter to be passed to the "is_valid" method as to how to handle subsequent validators on a field once one has failed, or to allow a parameter to be passed to the "register_validator" method that specifies that this validation should occur even if a prior validation failed. Or possibly a combination of both.

Any thoughts?
Back to top
View user's profile Send private message
IGonza
Smarty Rookie


Joined: 14 Jan 2007
Posts: 17

PostPosted: Sun Jan 14, 2007 1:34 pm    Post subject: Reply with quote

In this case it skips other validators for this field...
I thought 'halt' parameters was for it.
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:17 pm    Post subject: Reply with quote

IGonza wrote:
In this case it skips other validators for this field...
I thought 'halt' parameters was for it.


Halt, will terminate ALL other validation rules in the template, not only for the form field. So notice that we are talking about two different type of halts.

Broken Frame
Back to top
View user's profile Send private message
IGonza
Smarty Rookie


Joined: 14 Jan 2007
Posts: 17

PostPosted: Tue Apr 17, 2007 7:07 pm    Post subject: Reply with quote

Yes, I got it already.

Seems the solution from mckneisler is quite good.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Apr 23, 2007 2:41 pm    Post subject: Reply with quote

patch added to cvs, thanks!
Back to top
View user's profile Send private message Visit poster's website
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