|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
Nightstalker Smarty n00b
Joined: 03 Mar 2005 Posts: 3
|
Posted: Thu Mar 03, 2005 12:51 pm Post subject: SmartyValidate and transforms |
|
|
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 |
|
Nightstalker Smarty n00b
Joined: 03 Mar 2005 Posts: 3
|
Posted: Thu Mar 03, 2005 1:00 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 2:48 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 2:50 pm Post subject: |
|
|
Oops, the form will NOT redraw because it is valid but the form value is indeed lower case. |
|
Back to top |
|
Nightstalker Smarty n00b
Joined: 03 Mar 2005 Posts: 3
|
Posted: Thu Mar 03, 2005 2:57 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Mar 03, 2005 3:11 pm Post subject: |
|
|
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 |
|
|
|
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
|