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

smartValidate: multiple form validation question

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


Joined: 03 Jul 2004
Posts: 15
Location: New Zealand

PostPosted: Sun Jul 11, 2004 2:20 am    Post subject: smartValidate: multiple form validation question Reply with quote

I'm having a problem with SmartyValidate...

I have two forms on the same page, each with two fields.

Let's call them form A and form B, with fields A1, A2 on form A,
and B1 and B2 on form B. If it helps to visualise, form A is a
user id/password to log in as a customer, and form B is a user id/
password to log in as a dealer/merchant.

All four fields have a generic {validate ... } rule like this

Code:

{validate field="A1" form="A" criteria="notEmpty" transform="trim" message="please provide your user id" halt="yes"}

{validate field="A2" form="A" criteria="notEmpty" transform="trim" message="type something for a password" halt="yes"}


and so on...

Now, when I submit form A, I get a validation error showing for A1.

So far so good - that's that you'd expect. We have a halt="yes"
so we do not want to see an A2 validation message.

Then, doing nothing else, I submit form B.

But now that I submit form B, I get a validation error for A2 as
well as B1. I do not expect to see errors for a form I have
not submitted.

If I submit form A next, I get validation errors for A1 and B2.

Now, I can make the problem go away by resetting the form that is not being submitted:

[php]
if (isset($_POST['A_submit_button'])) {
if(SmartyValidate::is_valid($_POST,'A')) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty->display('main.tpl');
} else {
// error, redraw the form
SmartyValidate::register_form('B',true);
$smarty->assign($_POST);
$smarty->display('login.tpl');
}
}
[/php]

and


[php]
if (isset($_POST['B_submit_button'])) {
if(SmartyValidate::is_valid($_POST,'B')) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty->display('main2.tpl');
} else {
// error, redraw the form
SmartyValidate::register_form('A',true);
$smarty->assign($_POST);
$smarty->display('login.tpl');
}
}
[/php]



is this the answer? Am I expecting too much for smartyValidate to reset a form that wasn't called? Or should smartyvalidate not return "old" results for forms that are not being validated during that call?

Perhaps an "autoreset" option/setting that causes all non-called forms to automatically reset? Does someone actually WANT the state of any other form on the page kept?

I just cooked up a little modification - can I be so bold that something like this might be adoped? It works for me (so far Wink)

[php]
function is_valid(&$formvars, $form = 'default') {

if(!SmartyValidate::is_registered_form($form)) {
trigger_error("SmartyValidate: [is_valid] form '$form' is not registered.");
return false;
}

/* ********************** HACK STARTS HERE *************** */
// automatically reset all forms not being validated
$_tmp_arr = $_SESSION['SmartyValidate'];
foreach ($_tmp_arr as $_key => $_val) {
if ($form != $_key) {
SmartyValidate::register_form($_key,true);
}
}
unset($_tmp_arr);
/* ********************** HACK ENDS HERE ***************** */

// keep track of failed fields for current pass
static $_failed_fields = array();

$_ret = true;
$_sess =& $_SESSION['SmartyValidate'][$form]['validators'];
foreach($_sess as $_key => $_val) {
:
:
:
<snip>
[/php]

Thanks for your consideration...


Last edited by nzsmarty on Wed May 05, 2010 10:39 pm; edited 1 time in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jul 12, 2004 3:41 am    Post subject: Reply with quote

can you post the template? thanks
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Mon Jul 12, 2004 4:58 pm    Post subject: Reply with quote

Actually, if they are two separate forms then you should validate one or the other, not both. So in each form you could include a hidden value to identify the form:

Code:
<input type="hidden" name="formname" value="A">


Then in your code:

Code:
  if($_POST['formname'] == 'A') {
    if(SmartyValidate::is_valid($_POST,'A') { ... }
  } else {
    if(SmartyValidate::is_valid($_POST,'B') { ... }
  }
Back to top
View user's profile Send private message Visit poster's website
nzsmarty
Smarty Rookie


Joined: 03 Jul 2004
Posts: 15
Location: New Zealand

PostPosted: Mon Jul 12, 2004 8:05 pm    Post subject: Reply with quote

I agree Monte, which is exactly what I do (here comes to real code where I use three forms):

[php]
if (isset($_POST['form_btn_login_x'])) {
if(SmartyValidate::is_valid($_POST,'form_login')) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty-&gt;display('main.tpl');
} else {
// error, redraw the form
$smarty-&gt;assign($_POST);
$smarty-&gt;display('login.tpl');
}
}
if (isset($_POST['form_btn_view_x'])) {
if(SmartyValidate::is_valid($_POST,'form_view')) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty-&gt;display('view.tpl');
} else {
// error, redraw the form
$smarty-&gt;assign($_POST);
$smarty-&gt;display('login.tpl');
}
}
if (isset($_POST['form_btn_register_x'])) {
if(SmartyValidate::is_valid($_POST,'form_register')) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty-&gt;display('register.tpl');
} else {
// error, redraw the form
$smarty-&gt;assign($_POST);
$smarty-&gt;display('login.tpl');
}
}
[/php]

But only validating the submitted form doesn't make the oddity disappear.

It appears that the first {validate .. } rule that did not get to display for the PREVIOUS page submission (because of the halt="Yes" tag) gets to be shown for the CURRENT page submission (if submitting a different form on the same page).

My hack of clearing all data from any page not being called seems to work a treat for now, and elludes to the fact that data from a previous call is somehow persistent and gets picked up for the current call. If doing only one form per page, that isn't an issue - but it gets exposed on a multi form page.

When monitoring the $_SESSION data for SmartyValidate, you can see that any form that was previously set to FALSE, remains as FALSE when another form is being validated. Because the form isn't being validated, the data on it won't change.

Hmmmm, that makes it clearer to me....

The proper solution will most probably lie in the code that actually includes data for the { validate ... } statement in the page. It's probably there where it isn't "page-aware" and still picking up results from a previous call. Or, because of the halt="Yes" tags, the remaining { validate .. } statement is no longer processed, whereas at a minimum it would need to complete its processing without actually creating output?

The only odd thing is that it's only the 2nd { validate .. } that pops up again for the previously submitted form, but not the 3rd, 4th etc.

It doesn't just happen when submitting forms. When clicking a link which simply calls the page by name (say, a "Home" link), the same thing happens. So the is_valid doesn't even get called, and the previous form submission first-validate-that-was-not-displayed-last-time { validate .. } tag is then displayed also.

I'll have a look and see if I can find it.

(I certainly am getting to learn a lot about it all quickly Wink)

Warmest regards
Peter

________
CLASSIC


Last edited by nzsmarty on Sat Feb 12, 2011 6:46 pm; edited 2 times in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jul 12, 2004 8:13 pm    Post subject: Reply with quote

It would be helpful to see your template. Also, grab the latest CVS, there was a little bug fixed with the halt parameter when used with assign, I don't know if it affects what you're doing or not.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Mon Jul 12, 2004 8:16 pm    Post subject: Reply with quote

You should probably use elseif's in your code too, no sense checking any further once a particular button is recognized.
Back to top
View user's profile Send private message Visit poster's website
nzsmarty
Smarty Rookie


Joined: 03 Jul 2004
Posts: 15
Location: New Zealand

PostPosted: Mon Jul 12, 2004 8:29 pm    Post subject: Reply with quote

Quote:

It would be helpful to see your template.


It's been sent to you via email after your previous request. I'm not comfortable exposing an unpublished commercial idea to that extent in a public forum. It would be irresponsible to do so Wink.

Quote:

You should probably use elseif's in your code too, no sense checking any further once a particular button is recognized.


I come to PHP with habits from previous platforms. What I want is a switch statement that takes strings as case labels. I have a personal aversion to elseifs. But yes, you're right -time to get over it-, thanks for the suggestion.

________
Dental insurance advice

Last edited by nzsmarty on Sat Feb 12, 2011 6:47 pm; edited 2 times in total
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Mon Jul 12, 2004 9:04 pm    Post subject: Reply with quote

nzsmarty wrote:
It's been sent to you via email after your previous request. I'm not comfortable exposing an unpublished commercial idea to that extent in a public forum. It would be irresponsible to do so Wink.


I didn't get the e-mail, can you send it to my private mailbox on this forum?

nzsmarty wrote:
I come to PHP with habits from previous platforms. What I want is a switch statement that takes strings as case labels. I have a personal aversion to elseifs. But yes, you're right -time to get over it-, thanks for the suggestion.


AFAIK, PHP supports switch/case constructs too Wink
Back to top
View user's profile Send private message Visit poster's website
nzsmarty
Smarty Rookie


Joined: 03 Jul 2004
Posts: 15
Location: New Zealand

PostPosted: Mon Jul 12, 2004 9:19 pm    Post subject: Reply with quote

Quote:
I didn't get the e-mail, can you send it to my private mailbox on this forum?


You'll have it shortly.

-----
A bit off topic....

Quote:
AFAIK, PHP supports switch/case constructs too


Don't make fun of me! Laughing Of course I use them with numeric case labels...

However... by faking it similar to this you can have strings as case labels, but it's not pretty (From user contributed notes - PHP manual, switch section:)

[php]
switch (true){
case ( ereg ("stats",$userfile_name) ):
echo "processing stats";
process_stats();
break;
case ( ereg("prices",$userfile_name) ):
echo "processing prices";
process_prices();
break;
default:
echo = "File not recognized!!.";
}
[/php]

I can't help but suspect that has a bit of a peformance penalty when you run into a lot (50,100...) case sections... Shocked

On the other hand... I just found this:

Quote:
Using a if/elseif/elseif structure instead of switch/case/case can be 2 times faster (I have made a test)


Maybe it's time I get with the times Cool

.

________
HERBAL VAPORIZER

Last edited by nzsmarty on Sat Feb 12, 2011 6:47 pm; edited 2 times in total
Back to top
View user's profile Send private message
nzsmarty
Smarty Rookie


Joined: 03 Jul 2004
Posts: 15
Location: New Zealand

PostPosted: Tue Jul 13, 2004 7:53 am    Post subject: Reply with quote

For those following this discussion, the fix to my problem was already contained in the latest CVS.

Quote:
cvs -d :pserver:anonymous@cvs.phpinsider.com:/export/CVS login
cvs -d :pserver:anonymous@cvs.phpinsider.com:/export/CVS checkout SmartyValidate


And if, like me, you have left CVS on your to-do list, you can pull a copy for your platform from here

https://www.cvshome.org

Thanks to Monte for his help
________
MERCEDES-BENZ 230 HISTORY
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