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

Validating multiple forms

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


Joined: 04 May 2004
Posts: 10
Location: Stuttgart, Germany

PostPosted: Tue Jun 29, 2004 10:21 am    Post subject: Validating multiple forms Reply with quote

I encountered a problem when validating multiple forms on different pages with SmartyValidate.

If you have 2 or 3 stage forms one after another, the Class will do just fine. But if you try to click the "Back" link at the browser, the validation Array in the Session will get mixed up with different parameters from different forms.

Example :
form1 owns parameters "name1" and "address"
form 2 on the next page owns parameters "zip code" and "city"
If you click on a back link of the browser, form1 will also validate "zip code" and "city", which of course are inexistent, because they belong to form2 . These two parameters therefore have to be cleared from the session.

With the new SmartyValidate CVS version, its possible . Just checkout and update following two methods :

Code:

    function connect(&$smarty, $reset = false,$formname = 'default') {
        if(is_object($smarty) && ((get_class($smarty) == strtolower('smarty')) || is_subclass_of($smarty, strtolower('smarty')))) {
            SmartyValidate::_object_instance('Smarty', $smarty);
            SmartyValidate::register_form($formname, $reset);
        } else {
            trigger_error("SmartyValidate: [connect] I need a valid Smarty object.");
            return false;
        }
    } 


and
Code:

function disconnect($formname = 0) {
       if(!$formname)
        unset($_SESSION['SmartyValidate']);
        else unset($_SESSION['SmartyValidate'][$formname]);
        SmartyValidate::_object_instance('-', $_dummy);
    }   


The two methods can be called with a formname attribute now :
Code:

 SmartyValidate::connect($tpl,"","myform1");
 martyValidate::disconnect("myform1");


I know, it sounds a bit complicated, but you now have the possibility to clear different forms (form parameters) from the session and not only the default one. Dont forget to add the formname to the validation tags in this way :

{validate field="zipcode" form="myform2" criteria="notEmpty" message="<font color='red' size='3'><b>&!</b></font>"}

the form tag (in my example "myform2") has already been processed by the default SmartyValidate version in an excellent way. So you dont have to pay attention for this.

Thanks again for this great tool btw. Good job
_________________
See ya, Lemming
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Tue Jun 29, 2004 2:02 pm    Post subject: Reply with quote

I don't understand the problem, but maybe you are trying to use the 'default' form for both of your forms? That won't work.

The connect() method initializes SmartyValidate and registers the 'default' form. Typically you only use one form at a time, so the 'default' form would suffice for your validation.

If you are going to be juggling multiple forms, ideally you should register each of them separately (eg. don't use 'default' form) just to keep things straight.

example:

SmartyValidate::connect($smarty);
SmartyValidate::register_form('form1');
SmartyValidate::register_form('form2');

Now you have two separate forms registered, form1 and form2. In your validate tags, you specify which form each one belongs to:

{validate form="form1" field="address" criteria="notEmpty" message="..."}

{validate form="form2" field="address" criteria="notEmpty" message="..."}


Here are two separate validate tags for each form, both acting on a same-named field (address), yet they won't collide because they are registered to separate forms. When you call is_valid, be sure to supply it with the correct form name, and the POST values from the correct form:

// form1 has been submitted, validate it
if(SmartyValidate::is_valid($_POST,'form1')) { ... }

Neither form should collide. Does that cover the problem?
Back to top
View user's profile Send private message Visit poster's website
einlemming
Smarty Rookie


Joined: 04 May 2004
Posts: 10
Location: Stuttgart, Germany

PostPosted: Tue Jun 29, 2004 2:27 pm    Post subject: Reply with quote

Thats exactly the problem i was trying to explain.
This is, of course, another way to solve this problem.

Perhaps I just didn't manage to get the multiple forms to work with the register_form method.

Theres one point left. If I do a connect and perform some register_forms, the default form is registered in the session anyway. Note the text in bold.


function connect(&$smarty, $reset = false) {
if(is_object($smarty) && ((get_class($smarty) == strtolower('smarty')) || is_subclass_of($smarty, strtolower('smarty')))) {
SmartyValidate::_object_instance('Smarty', $smarty);
SmartyValidate::register_form('default', $reset);
} else {
trigger_error("SmartyValidate: [connect] I need a valid Smarty object.");
return false;
}
}

I think thats why I tried to mark the register_form methods as private methods, because the register_form is called within the connect method anyway. If you pass the formname with the connect() and disconnect() methods, you dont need the register_form and unregister form methods at all ( as public methods ) , because they are then handled internally. Additionaly the default form is not registered within the session.
Thanks for your support by the way !
_________________
See ya, Lemming
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Tue Jun 29, 2004 2:52 pm    Post subject: Reply with quote

The connect() method should be called once at the top of your script. this basically registers the smarty object with SmartyValidate. The reason that it registers the default form was for convenience, for those that don't validate more than one form at a time (which is the typical case.)

Instead of trying to make connect() deal with all of your forms, use form_register() and ignore the default form. I probably should have never added the default form registration to connect, but it is there because the typical usage case is one form, so that's one less function call in the PHP script.

example:

SmartyValidate::connect($smarty, empty($_POST));

That would reset the 'default' form in the case nothing was posted. Then the only other thing you would need in your PHP script is the validation:

if(SmartyValidate::is_valid($_POST)) { ... }

With multiple forms it gets slightly more compilcated because you need to register each form and start passing form names around. You can still use the 'default' form for one of your forms, then register the rest separately.
Back to top
View user's profile Send private message Visit poster's website
einlemming
Smarty Rookie


Joined: 04 May 2004
Posts: 10
Location: Stuttgart, Germany

PostPosted: Tue Jun 29, 2004 3:10 pm    Post subject: Reply with quote

You're right.
I think most users will use this class with only one form, so the parameters are sufficient with the original Class and additional forms can be added with the register_form and unregister_form methods. (just like you described)

Just one last note. Passing the form parameters within the modified connect and disconnect methods do not complicate the behaviour of the class, because I still pass the default form (see above) :
Code:

 function disconnect($formname = 0)
 ...
 function connect(&$smarty, $reset = false,$formname = 'default') {


So the class can still be used as supplied before.
But if you use multiple forms, it'll be two less function calls in the executed PHP script. (register_form and unregister_form)
_________________
See ya, Lemming
Back to top
View user's profile Send private message Visit poster's website
boots
Administrator


Joined: 16 Apr 2003
Posts: 5611
Location: Toronto, Canada

PostPosted: Tue Jun 29, 2004 3:19 pm    Post subject: Reply with quote

I just want to add that there may be an issue in the overridden disconnect method shown in the original post. In particular:

SmartyValidate::_object_instance('-', $_dummy);

will remove *all* held references to all assigned objects--not just the Smarty object. This may or may not be desired, so I thought I'd just point that out.
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Tue Jun 29, 2004 4:24 pm    Post subject: Reply with quote

I still don't see what purpose passing a form name to connect() serves.

SmartyValidate::connect($smarty, true, 'form1');

What does that accomplish? It registers both 'default' and 'form1'? What if you want three forms registered?

connect() handles registration for the 'default' form, all else is done with register_form() explicitly.
Back to top
View user's profile Send private message Visit poster's website
einlemming
Smarty Rookie


Joined: 04 May 2004
Posts: 10
Location: Stuttgart, Germany

PostPosted: Thu Jul 01, 2004 11:14 am    Post subject: Reply with quote

I just had the idea to make some things easier without modifying the usual behaviour of the application.

The primary precondition was to not register the default form at the session when using multiple forms. The default form of course has to be included if you just call the connect method without a formname. (just one form required)

The secondary precondition was to mark register_form and unregister_form as private (internal handling) . This saves two method calls when using the validation plugin with multiple forms.

Last but not least the third and imho the most important condition is that the user of the Validation plugin can use the plugin as supplied before. For this purpose I included default parameters for the formname when using the connect and disconnect methods. The class therefore behaves as usual if you dont use a formname at all.

Sorry if I got mixed up. This forum helped me a lot. Thanks to all for your hard work.
_________________
See ya, Lemming
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