View previous topic :: View next topic |
Author |
Message |
vchris Smarty Rookie
Joined: 03 Mar 2009 Posts: 32
|
Posted: Mon Mar 16, 2009 6:38 pm Post subject: SmartyValidate issues |
|
|
Let's say the form doesn't validate, I fix my input and it submits and I do a back, it causes errors. Warning: Smarty error: validate: validator id 'keyword' is not registered. in /path/libs/Smarty.class.php on line 1092. Is there a way to fix that?
How can I select a form variable after the validation? Should I just go with $_POST vars? |
|
Back to top |
|
vchris Smarty Rookie
Joined: 03 Mar 2009 Posts: 32
|
Posted: Tue Mar 17, 2009 1:09 pm Post subject: |
|
|
I have a search form, then a results page, then a item specific page. The problem seems to occur when I go down to the item page and do a back. I resubmit the form info and it creates the above errors.
How can I have it so if the user can does a back, he won't have to resend form info and he'll be able to stay on the results page? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 17, 2009 1:20 pm Post subject: |
|
|
After the form is submitted, do an HTTP redirect to the results page. Then the back button will not result in a form resubmit. |
|
Back to top |
|
vchris Smarty Rookie
Joined: 03 Mar 2009 Posts: 32
|
Posted: Tue Mar 24, 2009 6:42 pm Post subject: |
|
|
I have a contact page. If I enter the correct data in the fields it'll submit and if I do a back, I get no errors. If I enter incorrect data in the fields, correct the data and resubmit, it'll submit. If I do a back I get these errors:
Warning: Smarty error: validate: validator id 'name' is not registered. in /path/libs/Smarty.class.php on line 1092
Warning: Smarty error: validate: validator id 'email' is not registered. in /path/libs/Smarty.class.php on line 1092
Warning: Smarty error: validate: validator id 'subject' is not registered. in /path/libs/Smarty.class.php on line 1092
Warning: Smarty error: validate: validator id 'message' is not registered. in /path/libs/Smarty.class.php on line 1092
Is there some initialization I need to do somewhere?
Code: |
<?php
session_start();
require 'libs/Smarty.class.php';
require 'libs/Trader.class.php';
require 'libs/SmartyValidate.class.php';
$smarty =& new Smarty;
$trader = new Trader;
if(empty($_POST)) {
// new form, we (re)set the session data
SmartyValidate::connect($smarty, true);
// register our validators
SmartyValidate::register_validator('name', 'name', 'notEmpty',
false, false, 'trim');
SmartyValidate::register_validator('email', 'email', 'isEmail',
false, false, 'trim');
SmartyValidate::register_validator('subject', 'subject', 'notEmpty',
false, false, 'trim');
SmartyValidate::register_validator('message', 'message', 'notEmpty',
false, false, 'trim');
// display form
$smarty->display('contact.tpl');
} else {
// validate after a POST
SmartyValidate::connect($smarty);
if(SmartyValidate::is_valid($_POST)) {
// no errors, done with SmartyValidate
SmartyValidate::disconnect();
$smarty->display('contact_success.tpl');
// send email
$trader->send_mail($name, $email, $subject, $message);
} else {
// error, redraw the form
$smarty->assign($_POST);
$smarty->display('contact.tpl');
}
}
?>
|
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Tue Mar 24, 2009 6:46 pm Post subject: |
|
|
SmartyValidate::disconnect(); unregisters all validators. If you visit the form again, they need to be reregistered first. |
|
Back to top |
|
vchris Smarty Rookie
Joined: 03 Mar 2009 Posts: 32
|
Posted: Wed Mar 25, 2009 2:04 pm Post subject: |
|
|
so how would I fix this issue?
I changed the first line to:
if(empty($_POST) && !SmartyValidate::is_registered_form('contactform')) {
doesn't seem to work.
EDIT: I fixed it with this line:
if(empty($_POST) || !SmartyValidate::is_registered_form('default')) {
Thanks for your help! |
|
Back to top |
|
mnew707 Smarty n00b
Joined: 13 Mar 2010 Posts: 1
|
Posted: Sat Mar 13, 2010 9:12 pm Post subject: SmartyValidate issues |
|
|
I'm dealing with the exact same issue described here. However, when I try the fix in the last thread, I still get the errors when hitting the back button. Here's what I tried:
if(empty($_POST) || !SmartyValidate::is_registered_form('default')) {
if(empty($_POST) || !SmartyValidate::is_registered_form()) {
and tried this syntax:
if(empty($_POST) || !$validator->is_registered_form('default')) {
if(empty($_POST) || !$validator->is_registered_form()) {
Why does it still bomb for me?
I'm thinking of setting a session variable to flag a successful post of form #1 so that when it's revisited after a "back" from form #2, I can tell smarty to treat form #1 as a brand new visit and re-register the variables.
Something like:
$_SESSION['form1'] = "visited"; // I would add this check in the condition for registering the variables
I have tried launching a "sucess" message after good validation in form #1 and providing a redirect button to form #2 but I still get the smarty errors for registering the variable when hitting the "back" button from form #2.
There are multiple forms in this survey so each form is redirected to the next upon sucess with a new script and template. |
|
Back to top |
|
sayolala Smarty n00b
Joined: 10 May 2010 Posts: 2
|
|
Back to top |
|
|