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 transforms

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


Joined: 03 Mar 2005
Posts: 3

PostPosted: Thu Mar 03, 2005 12:51 pm    Post subject: SmartyValidate and transforms Reply with quote

Hi,

Is it possible to instruct SmartyValidate to set the input values to their transformed value?

I am having a problem on a form where the input for 'user_firstname' and 'user_lastname' must be lowercase. I wrote a transform to call strtolower, and a validation function that checks against valid characters. But once SmartyValidate declares the form valid, the input values are still non-lowercased, which forces me to "manually" recall all transform funcs in the php code.

Is there a way to avoid that repetition?

Thanks in advance,
Dave
Back to top
View user's profile Send private message
Nightstalker
Smarty n00b


Joined: 03 Mar 2005
Posts: 3

PostPosted: Thu Mar 03, 2005 1:00 pm    Post subject: Reply with quote

Hmm, looking at the source code for is_valid(), this is already done as the form values are passed by reference. Any idea as to what could be my problem in that case?
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Mar 03, 2005 2:48 pm    Post subject: Reply with quote

I tried this and works for me:

validate_transform.lower.php (in plugins_dir):

[php:1:c290f9ffb9]function smarty_validate_transform_lower($value, $params, &$formvars) {
return strtolower($value);
}[/php:1:c290f9ffb9]

index.php:

[php:1:c290f9ffb9]session_start();

require './smartylib/Smarty.class.php';
require './libs/SmartyValidate.class.php';

$smarty =& new Smarty();
SmartyValidate::connect($smarty);

switch($_REQUEST['fp']) {
case 'myform':
if(SmartyValidate::is_valid($_POST)) {
$smarty->display('success.tpl');
} else {
$smarty->assign($_POST);
$smarty->display('form.tpl');
}
break;
default:
SmartyValidate::connect($smarty, true);
$smarty->display('form.tpl');
break;

}
[/php:1:c290f9ffb9]

form.tpl (in template_dir):

Code:
{validate field="user" criteria="notEmpty" transform="lower" message="user cannot be empty"}

<form name="form1" method="post" action="{$SCRIPT_NAME}">
User: <input type="text" name="user" value="{$user}" size="20"><br />
<input type="submit">
<input type="hidden" name="fp" value="myform">
</form>



Type in a user name with uppercase, the form redraws with the user name lowercase.
Back to top
View user's profile Send private message Visit poster's website
mohrt
Administrator


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

PostPosted: Thu Mar 03, 2005 2:50 pm    Post subject: Reply with quote

Oops, the form will NOT redraw because it is valid Wink but the form value is indeed lower case.
Back to top
View user's profile Send private message Visit poster's website
Nightstalker
Smarty n00b


Joined: 03 Mar 2005
Posts: 3

PostPosted: Thu Mar 03, 2005 2:57 pm    Post subject: Reply with quote

Okay, never mind. My problem was (1) badly naming the plugin (the doc says to name it smarty_validate_transform_tolower.php), and session data lying around because I wasn't resetting the form validation data when "starting over", so validation was still done against the old criteria, which didn't have the tolower transform (hence no "plugin not found" error).

Corrected all of that, and everything works fine, thanks!
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Thu Mar 03, 2005 3:11 pm    Post subject: Reply with quote

documentation error! I'll get that fixed, thanks. (you do NOT want smarty in the filename, but you do in the function name for name space precautions)
Back to top
View user's profile Send private message Visit poster's website
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