View previous topic :: View next topic |
Author |
Message |
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu May 06, 2004 9:37 pm Post subject: SmartyValidate: a simple yet powerful form validation plugin |
|
|
This plugin evolved from a need most every programmer runs into: form validation. This can be a tedious and time consuming task. The idea was to leverage the Smarty environment to make form validation as easy yet powerful as possible. This was accomplished by abstracting the validation grunt work and reducing it to simple validation criteria and messages in the template.
This isn't extensively tested, so please let me know if you use it, improve it, find bugs, etc. Enjoy!
http://www.phpinsider.com/php/code/SmartyValidate/ |
|
Back to top |
|
Justin Smarty Regular
Joined: 07 May 2003 Posts: 38 Location: Vilnius, Lithuania
|
Posted: Fri May 07, 2004 7:01 am Post subject: |
|
|
this is what I have found in documentation:
Quote: |
isCCExpDate
-----------
example:
{validate field="ccexp" criteria="isCCExpDate" message="..."}
"isEmail": is valid credit card expiration date.
isDate
------
example:
{validate field="startDate" criteria="isDate" message="..."}
"isEmail": is valid Date (parsible by strtotime()).
|
what "isEmail" are doing there? should be isCCExpDate and isDate, I guess. the same is with "isRange" _________________ http://www.baubas.net |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri May 07, 2004 1:31 pm Post subject: |
|
|
A cut-and-paste error when throwing docs together, I'll get that fixed. I'm also going to add a function registration method so you can lock down what functions can be called with it. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
|
Back to top |
|
khan2265 Smarty Rookie
Joined: 07 May 2004 Posts: 21
|
Posted: Fri May 07, 2004 4:59 pm Post subject: |
|
|
plugin is nice.
any chance it would be bundled in the next smarty release? |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri May 07, 2004 5:06 pm Post subject: |
|
|
Probably not. It will eventually become part of a cohesive plugin repository once we get that thought out and put together. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri May 07, 2004 7:32 pm Post subject: |
|
|
Yet another minor update, I decided to make inCustom function calls require registration (instead of allowing any when none are registered.) v1.2 is now available.
http://www.phpinsider.com/php/code/SmartyValidate/ |
|
Back to top |
|
pt2002 Smarty Regular
Joined: 05 May 2003 Posts: 89 Location: Porto, Portugal
|
Posted: Fri May 21, 2004 9:23 am Post subject: New param |
|
|
Hello
Is there a way to pass a new param to format the eerro message?
example: style={font: verdana 11px red} |
|
Back to top |
|
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Fri May 21, 2004 5:21 pm Post subject: |
|
|
'lo i just wanted to let you know that i removed my javascript form validation and changed it with this class. It rocks!
I changed the function smarty.validate.php so it accepts a custom template.
usage:
{validate field="companycode" criteria="notEmpty" message="You forgot your company code." tpl_file="custom_error.tpl"}
the tpl_file is searched in your tpl path.
Code: | <?php
function smarty_function_validate($params, &$smarty) {
if (!class_exists('SmartyValidate')) {
$smarty->trigger_error("validate: missing SmartyValidate class");
return;
}
if (strlen($params['field']) == 0) {
$smarty->trigger_error("validate: missing 'field' parameter");
return;
}
if (strlen($params['criteria']) == 0) {
$smarty->trigger_error("validate: missing 'criteria' parameter");
return;
}
if(isset($params['trim'])) {
$params['trim'] = SmartyValidate::booleanize($params['trim']);
} else {
$params['trim'] = false;
}
if(isset($params['empty'])) {
$params['empty'] = SmartyValidate::booleanize($params['empty']);
} else {
$params['empty'] = false;
}
if(!isset($params['tpl_file'])) {
$params['tpl_file'] = '';
}
switch($params['criteria']) {
case 'notEmpty':
case 'isInt':
case 'isFloat':
case 'isNumber':
case 'isPrice':
case 'isEmail':
case 'isCCNum':
case 'isCCExpDate':
case 'isDate':
break;
case 'isEqual':
if (strlen($params['field2']) == 0) {
$smarty->trigger_error("validate: isEqual missing 'field2' parameter");
return;
}
break;
case 'isRange':
if (strlen($params['low']) == 0) {
$smarty->trigger_error("validate: missing 'low' parameter");
return;
}
if (strlen($params['high']) == 0) {
$smarty->trigger_error("validate: missing 'high' parameter");
return;
}
break;
case 'isRegExp':
if (strlen($params['expression']) == 0) {
$smarty->trigger_error("validate: isRegExp missing 'expression' parameter");
return;
}
break;
case 'isCustom':
if (strlen($params['function']) == 0) {
$smarty->trigger_error("validate: isCustom missing 'function' parameter");
return;
}
if(!preg_match('!^\w+(::\w+)?$!', $params['function'])) {
$smarty->trigger_error("validate: isCustom invalid 'function' parameter");
return;
}
break;
default:
$smarty->trigger_error("validate: unknown criteria '" . $params['criteria'] . "'");
return;
break;
}
$_form = isset($params['form']) ? $params['form'] : 'default';
$_sess =& $_SESSION['SmartyValidate'][$_form]['validators'];
$_found = false;
foreach($_sess as $_key => $_field) {
if($_field['field'] == $params['field']
&& $_field['criteria'] == $params['criteria']) {
// field exists
$_found = true;
if(isset($_sess[$_key]['valid'])
&& !$_sess[$_key]['valid']) {
// not valid, show error and reset
if(isset($params['assign'])) {
$smarty->assign($params['assign'], $params['add_pre'].$_sess[$_key]['message'].$params['add_aft']);
} else {
if (!empty($params['tpl_file'])) {
$x = $smarty;
$x->clear_all_assign();
$x->assign('error', $_sess[$_key]['message']);
$str = $x->fetch($params['tpl_file']);
unset($x);
} else {
$str = $_sess[$_key]['message'];
}
echo $str;
}
$_sess[$_key]['valid'] = null;
break;
}
}
}
if(!$_found) {
// create
$_sess[] = $params;
}
}
?> |
|
|
Back to top |
|
electr0n Smarty Rookie
Joined: 26 Mar 2004 Posts: 32 Location: Germany
|
Posted: Sat May 22, 2004 11:56 am Post subject: Bug! |
|
|
I've found a bug in the SmartyValidate.class.php, trim doesn't work.
Change the line 58 in SmartyValidate.class.php from
$formvars[$_field] = trim($formvars[$_field]);
to
$formvars[$_val['field']] = trim($formvars[$_val['field']]);
and it'll work. |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 26, 2004 2:00 pm Post subject: |
|
|
Version 1.3 is now available.
http://www.phpinsider.com/php/code/SmartyValidate/
ChangeLog:
* added isLength validator
* stop validation after one validator fails (per field)
* fix trim() functionality
* fix _is_float function call (typo)
* fix foreach() error condition |
|
Back to top |
|
bbwdd_com Smarty n00b
Joined: 26 May 2004 Posts: 1
|
Posted: Wed May 26, 2004 4:44 pm Post subject: very good! |
|
|
i like it! _________________
|
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 26, 2004 9:56 pm Post subject: Version 1.4 now available |
|
|
Moving right along, version 1.4 is now available.
http://www.phpinsider.com/php/code/SmartyValidate/
ChangeLog:
* added third parameter to custom functions to access all available attrs.
* fixed bug in is_registered_function() method (only affects custom funcs)
* fixed incorrect example of is_valid() in the docs |
|
Back to top |
|
xces Smarty Regular
Joined: 09 Apr 2004 Posts: 77
|
Posted: Wed May 26, 2004 10:23 pm Post subject: Re: Version 1.4 now available |
|
|
I would like to see some credit for my suggested bugs.. but anyway..
mohrt wrote: | Moving right along, version 1.4 is now available... |
---------------------------------------------------------------------------
There is still a minor error in the is_custom function:
---------------------------------------------------------------------------
[php:1:b4184343e6] case 'isCustom':
$_sess[$_key]['valid'] =
SmartyValidate::_is_custom(
$formvars[$_field],
$_sess[$_key]['function'],
$_empty,
$_sess[$_key]);[/php:1:b4184343e6]
---------------------------------------------------------------------------
should read:
---------------------------------------------------------------------------
(you still forget to pass the form name)
[php:1:b4184343e6] case 'isCustom':
$_sess[$_key]['valid'] =
SmartyValidate::_is_custom(
$form,
$formvars[$_field],
$_sess[$_key]['function'],
$_empty,
$_sess[$_key]);[/php:1:b4184343e6]
---------------------------------------------------------------------------
and this code:
---------------------------------------------------------------------------
[php:1:b4184343e6] /**
* test if a value is a valid range
*
* @param string $value the value being tested
* @param string $fuction the function to test against
* @param boolean $empty if field can be empty
*/
function _is_custom($value, $function, $empty = false, &$params) {
if(SmartyValidate::is_registered_function($function)) {
if(!function_exists($function)) {
trigger_error("SmartyValidate: function '$function' does not exist.");
return false;
}
return $function($value, $empty, $params);
} else {
trigger_error("SmartyValidate: function '$function' is not registered.");
return false;
}
}[/php:1:b4184343e6]
---------------------------------------------------------------------------
should read:
---------------------------------------------------------------------------
[php:1:b4184343e6] /**
* test if a value is a valid range
*
* @param string $value the value being tested
* @param string $fuction the function to test against
* @param boolean $empty if field can be empty
*/
function _is_custom($form, $value, $function, $empty = false, &$params) {
if(SmartyValidate::is_registered_function($function, $form) {
if(!function_exists($function)) {
trigger_error("SmartyValidate: function '$function' does not exist.");
return false;
}
return $function($value, $empty, $params);
} else {
trigger_error("SmartyValidate: function '$function' is not registered.");
return false;
}
}[/php:1:b4184343e6]
---------------------------------------------------------------------------
Custom error templates
---------------------------------------------------------------------------
If you want custom error templates like i do... Change the following code in function.validate.php (on line 118) ...
[php:1:b4184343e6] echo $_sess[$_key]['message'];[/php:1:b4184343e6]
... into this:
[php:1:b4184343e6] if (!empty($params['tpl_file'])) {
$x = $smarty;
$x->clear_all_assign();
$x->assign('error', $_sess[$_key]['message']);
$str = $x->fetch($params['tpl_file']);
unset($x);
} else {
$str = $_sess[$_key]['message'];
}
echo $str;[/php:1:b4184343e6]
And you can use a custom tpl (usefull if you want all errors to look thesame) An example tpl (save in your smarty template directory as frm_error.tpl) might look like this:
[php:1:b4184343e6]<span class="frm_error">{$error}</span>[/php:1:b4184343e6]
---------------------------------------------------------------------------
Example of use:
---------------------------------------------------------------------------
[php:1:b4184343e6]{validate ... ... ... tplfile="frm_error.tpl"}[/php:1:b4184343e6] |
|
Back to top |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Wed May 26, 2004 10:40 pm Post subject: Re: Version 1.4 now available |
|
|
xces wrote: | I would like to see some credit for my suggested bugs.. but anyway.. |
You mean the custom error templates? It's an interesting hack, but I'm hesitant to add that to the validator class. You can pretty easily handle error formatting in the template already. Example:
Code: | {validate field="foobar" criteria="notEmpty" message="can't be empty" assign="error"}
{include file="error.tpl" error=$error}
|
Then in the error.tpl file:
Code: | {if $error ne ""}
... do formatting here ...
{/if}
|
This requires one extra step in the template, but keeps the validator simple and flexible. But in any event if someone wants to use your method, it's posted here for the world to use Thanks for your input and contributions! |
|
Back to top |
|
|