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 problem: multiple forms on one page

 
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
tangfucius
Smarty Rookie


Joined: 30 Sep 2004
Posts: 16

PostPosted: Thu Sep 08, 2005 2:08 pm    Post subject: SmartyValidate problem: multiple forms on one page Reply with quote

I have two forms on one page, and I registered both forms and their validators separately by the form names. However when I validate one form, and there is invalid data from that form. The validator error messages show up for all of the validators on the page, not just the validators associated with the form I did SmartyValidate:is_valid on. How can I fix this?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 08, 2005 2:17 pm    Post subject: Reply with quote

I'm not sure that is supported since all the validators are getting tested on the page redraw. Try using one form id for all of them, and setting separate "page" values for the separate validators. Then set the appropriate page value for the form you are validating. The other validators should get ignored.

Code:
// form 1
{validate page="1" id="foo1" ...}
{validate page="1" id="foo2" ...}

// form 2
{validate page="2" id="foo3" ...}
{validate page="2" id="foo4" ...}


[php:1:f928987806]SmartyValidate::set_page('2');
SmartyValidate::is_valid($_POST);
[/php:1:f928987806]
Back to top
View user's profile Send private message Visit poster's website
tangfucius
Smarty Rookie


Joined: 30 Sep 2004
Posts: 16

PostPosted: Thu Sep 08, 2005 3:34 pm    Post subject: Reply with quote

Thank you for the reply!

I tried your suggestion, no error messages show, but SmartyValidate::is_valid returns false even if the data is valid.
Also when the data is invalid, none of the validator error messages would show.
Is there anything else special I need to do to use the page tag?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 08, 2005 3:38 pm    Post subject: Reply with quote

You shouldn't have to do much. Make sure each {validator} in the template has the appropriate page value assigned to it, then set the page value on the PHP side before using is_valid(). Maybe you can post a minimal test-case that shows the problem.
Back to top
View user's profile Send private message Visit poster's website
tangfucius
Smarty Rookie


Joined: 30 Sep 2004
Posts: 16

PostPosted: Thu Sep 08, 2005 3:45 pm    Post subject: Reply with quote

here goes the code

Code:
      if ( isset($_POST['savenext']) ) { //if data submitted
         //use smartvalidate to validate data
         SmartyValidate::connect($smarty);
         SmartyValidate::set_page('1');
         if( SmartyValidate::is_valid($_POST) ) {
            //if no input errors, clean up SmartyValidate
            SmartyValidate::disconnect();
            //do something else
         } else { //user input validation failed
            set_page_mode('EDIT_ERROR');
         }
      } else {
         set_page_mode('EDIT');
          //initialize SmartyValidate and register validators
          SmartyValidate::disconnect();
         SmartyValidate::connect($smarty, true);
          SmartyValidate::register_validator('body_background_color', 'body_background_color', 'isHTMLColor');
          SmartyValidate::register_validator('body_color', 'body_color', 'isHTMLColor');
          //logo attachment validators
          SmartyValidate::register_validator('host_image_type', 'host_image:jpg,gif,png', 'isFileType');
          SmartyValidate::register_validator('host_image_size', 'host_image:50k', 'isFileSize');
      }


[/code]
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 08, 2005 4:03 pm    Post subject: Reply with quote

You might be having a problem with disconnect(). Don't call disconnect() until ALL validation is complete. You don't really have to call disconnect() at all, it is only for session data cleanup. When you call disconnect(), you lose all validation states for both forms.

so do something like:

1) register ALL your validators in PHP

2) make sure each {validate} statement has the appropriate page value in the template

3) during the validation cycle, set the appropriate set_page() value before calling is_valid on the PHP side (looks like you already do that.)

4) when everything is complete/valid, you can call disconnect()
Back to top
View user's profile Send private message Visit poster's website
tangfucius
Smarty Rookie


Joined: 30 Sep 2004
Posts: 16

PostPosted: Thu Sep 08, 2005 5:05 pm    Post subject: Reply with quote

I did exactly what you suggested, but with no luck.

I looked into the source code of SmartyValidate.class.php,
and change line 450 from (inside of the set_page) function

$_SESSION['SmartyValidate'][$form]['is_init'] = true;

to

$_SESSION['SmartyValidate'][$form]['is_init'] = false;

(however from my understanding, this will break the functionality for multi-page forms)

and now it validates the first form asscoiated with page 1,
however when i try to post the second form that's associated with page 2,
it validates if data is valid, however when the data is invalid, all the validators for form1/page 1 still get shown, just like the initial problem I had with multiple forms on the same page in the initial post.

The problem is not within the SmartyValidate class, it's the function.validate.php that does not take care of the multi-page form concept on the same page, because it does it by form but not page.
So each {validate} tag on that page will get processed and echo out errors if valid is not set.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Sep 08, 2005 7:24 pm    Post subject: Reply with quote

Yeah it doesn't sound like trying to validate two separate forms on the same page is working too well.

You could probably register form 1, then after it is validated, destroy that form registration and then register form 2, and validate that one. I don't know if that is the behavior you are after though. Maybe put your forms on two separate pages Wink
Back to top
View user's profile Send private message Visit poster's website
Tomserv
Smarty n00b


Joined: 18 Sep 2007
Posts: 2

PostPosted: Tue Sep 18, 2007 5:08 pm    Post subject: Reply with quote

I added this line
Code:
if(isset($_POST['form_to_validate']) AND $_POST['form_to_validate'] !== $params['form']) return;

right below the
Code:
    if(isset($params['form']))
    {
       [...];
    }

block in the beginning of function.validate.php and equipped my forms with a hidden field like
Code:
<input type="hidden" name="form_to_validate" value="my_form" />
.

This way i get multiple forms running on one page and validation is only shown for the submitted form.

Not tested very thoroughly but seems to work so far... Very Happy Question
Back to top
View user's profile Send private message
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