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 and multi-page forms examples

 
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
MrNiceguyD
Smarty n00b


Joined: 11 Apr 2005
Posts: 4
Location: The Hague, Netherlands

PostPosted: Mon Apr 11, 2005 11:13 am    Post subject: SmartyValidate and multi-page forms examples Reply with quote

Does anyone has an (simple)example of how to use SmartyValidate with a multipage form? I am using the latest versions of SmartyValidate(2.4.0) and Smarty(2.6.9). I am using SmartyValidate on to validate all forms and it works great, i just cant figure out how to get multi-page forms to work.
Any help would be greatly appreciated.

Best regards,

Davy
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Apr 11, 2005 1:16 pm    Post subject: Reply with quote

1) put your form across multiple templates (each page)

2) on each page you have a validator, set the page number:
{validate page="1" ...}

3) prior to displaying a page, set the SmartyValidate::set_page('1')

So for example:

SmartyValidate::set_page('1');
$smarty->display('form_p1.tpl');

// do validation cycle for page 1, then

SmartyValidate::set_page('2');
$smarty->display('form_p2.tpl');

// etc...
Back to top
View user's profile Send private message Visit poster's website
MrNiceguyD
Smarty n00b


Joined: 11 Apr 2005
Posts: 4
Location: The Hague, Netherlands

PostPosted: Mon Apr 11, 2005 2:06 pm    Post subject: Reply with quote

Thanks for you quick reply Very Happy

I have two templates and added the page attribute to the validators like {validate page="1" ...} etc. And in my php code i have done SmartyValidate::set_page('1') and SmartyValidate::set_page('1') prior to displaying the form but i stil get 2 forms on my page. How would u let the form know on what page you are and which form to validate ($_GET or $_SESSION)? btw is it okay to use:

$validator->set_page('2'); instead of SmartyValidate::set_page('1') , ofcourse after creating an instance of the SmaryValidate class first?

Here is my code:

[php:1:657ebe639d]<?php
session_start();

require('../../lib/demo/setup.php');
require('../../lib/Smarty/SmartyValidate.class.php');

$smarty =& new Smarty_Demo();
$validator =& new SmartyValidate();

if(empty($_POST)) {
// new form, we (re)set the session data
$validator->connect($smarty, true);

// register our validators
$validator->register_validator('username', 'UserName', 'notEmpty', false, false, 'trim');
$validator->register_validator('password', 'password:password2', 'isEqual');
// display form
$validator->set_page('1');
$smarty->display('signup_form1.tpl');

// register our validators
$validator->register_validator('firstname', 'FirstName', 'notEmpty', false, false, 'trim');
$validator->register_validator('lastname', 'LastName', 'notEmpty', false, false, 'trim');
$validator->register_validator('emailempty', 'Email', 'notEmpty', false, false, 'trim');
$validator->register_validator('emailvalid', 'Email', 'isEmail', false, false, 'trim');
$validator->register_validator('phone', 'Phone', 'isNumber', true, false, 'trim');
// display form
$validator->set_page('2');
$smarty->display('signup_form2.tpl');
} else {
// validate after a POST
$validator->connect($smarty);
if($validator->is_valid($_POST)) {
// no errors, done with SmartyValidate
$validator->disconnect();
$smarty->display('success.tpl');
} else {
// error, redraw the form
$smarty->assign($_POST);
$validator->set_page('1');
$smarty->display('signup_form1.tpl');
}
}
?>[/php:1:657ebe639d]

I know i am probably forgetting something here.. since it isnt working?
Thanks for your time

Best regards,

Davy
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Apr 11, 2005 2:38 pm    Post subject: Reply with quote

They both display because you are displaying both forms in your logic. You need to display page 1 first, than after that validates, display page 2, etc. Something like (untested):

[php:1:13a63ee414]$smarty =& new Smarty;

switch($_REQUEST['page']) {
case "1":
// coming from first form page
SmartyValidate::connect($smarty);
if(SmartyValidate::is_valid($_POST)) {
// first page valid, display second page
SmartyValidate::set_page('2');
$smarty->display('form_p2.tpl');
} else {
// error, redraw first page
$smarty->assign($_POST);
$smarty->display('form_p1.tpl');
}
break;
case "2":
// coming from second page
SmartyValidate::connect($smarty);
if($smarty->is_valid($_POST)) {
// second page is valid, we're done
SmartyValidate::disconnect();
$smarty->display('main_menu.tpl');
} else {
// error on second page, redraw
$smarty->assign($_POST);
$smarty->display('form_p2.tpl');
}
break;
default:
// first time in, or new form
SmartyValidate::connect($smarty,true);
// register all validators
$validator->register_validator('username', 'UserName', 'notEmpty', false, false, 'trim');
$validator->register_validator('password', 'password:password2', 'isEqual');
$validator->register_validator('firstname', 'FirstName', 'notEmpty', false, false, 'trim');
$validator->register_validator('lastname', 'LastName', 'notEmpty', false, false, 'trim');
$validator->register_validator('emailempty', 'Email', 'notEmpty', false, false, 'trim');
$validator->register_validator('emailvalid', 'Email', 'isEmail', false, false, 'trim');
$validator->register_validator('phone', 'Phone', 'isNumber', true, false, 'trim');
// set page, draw form
SmartyValidate::set_page('1');
$smarty->display('form_p1.tpl');
break;
}[/php:1:13a63ee414]

Notice you need to keep track of what page you are on in the form:

Code:
<form ...>
...
<input type="hidden" name="page" value="1">
</form>


or

Code:
<form method=post action="{$SCRIPT_NAME}?page=1">
...
</form>
Back to top
View user's profile Send private message Visit poster's website
MrNiceguyD
Smarty n00b


Joined: 11 Apr 2005
Posts: 4
Location: The Hague, Netherlands

PostPosted: Mon Apr 11, 2005 3:26 pm    Post subject: Reply with quote

Thanks again Very Happy

How stupid i should`ve seen that lol. I gues its monday...
I have modified my code but i get errors:

Warning: Smarty error: validate: validator id 'username' is not registered. in C:\Projects\davy\lib\Smarty\Smarty.class.php on line 1088

But i do have {validate page="1" id="username" message="<div class=\"error\">User Name Cannot Be Empty</div>"} in my first template.

I cleared my sessions in my browser and my cache, any idea`s?

[php:1:ccd20b54b2]<?php
session_start();

require('../../lib/demo/setup.php');
require('../../lib/Smarty/SmartyValidate.class.php');

$smarty =& new Smarty_Demo();
$validator =& new SmartyValidate();

switch ($_POST['page']) {
case "1":
// coming from first form page
$validator->connect($smarty);

// register our validators
$validator->set_page('1');
$validator->register_validator('username', 'UserName', 'notEmpty', false, false, 'trim');
$validator->register_validator('password', 'password:password2', 'isEqual');

if($validator->is_valid($_POST)) {
// no errors, show page 2
$validator->set_page('2');
$smarty->display('signup_form2.tpl');
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('signup_form1.tpl');
}
break;

case "2":
// coming from second form page
$validator->connect($smarty);

// register our validators
$validator->set_page('2');
$validator->register_validator('firstname', 'FirstName', 'notEmpty', false, false, 'trim');
$validator->register_validator('lastname', 'LastName', 'notEmpty', false, false, 'trim');
$validator->register_validator('emailempty', 'Email', 'notEmpty', false, false, 'trim');
$validator->register_validator('emailvalid', 'Email', 'isEmail', false, false, 'trim');
$validator->register_validator('phone', 'Phone', 'isNumber', true, false, 'trim');

if($validator->is_valid($_POST)) {
// no errors, done with SmartyValidate
$validator->disconnect();
$smarty->display('success.tpl');
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('signup_form2.tpl');
}
break;

default:
// first time in, or new form
$validator->connect($smarty, true);

// register our validators
$validator->set_page('1');
$validator->register_validator('username', 'UserName', 'notEmpty', false, false, 'trim');
$validator->register_validator('password', 'password:password2', 'isEqual');

// display form
$smarty->display('signup_form1.tpl');
break;
}

?>

?>[/php:1:ccd20b54b2]

Thanks again for your time.

Best regards,

Davy
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Apr 11, 2005 3:33 pm    Post subject: Reply with quote

Try registering all your validators only once, like in the example I gave. You certainly don't want to execute register_validator() inside of your validation cycle.
Back to top
View user's profile Send private message Visit poster's website
MrNiceguyD
Smarty n00b


Joined: 11 Apr 2005
Posts: 4
Location: The Hague, Netherlands

PostPosted: Mon Apr 11, 2005 4:04 pm    Post subject: Reply with quote

Thanks again Monte,

It works great now Very Happy Very Happy

Best regards,

Davy Very Happy
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