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

SmartyValidate: Issues with empty variables

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


Joined: 19 Jul 2004
Posts: 12

PostPosted: Fri Jul 23, 2004 9:09 pm    Post subject: SmartyValidate: Issues with empty variables Reply with quote

Using SmartyValidate-2.2 / Smarty-2.6.3


NOTE: this post has been and continues to be subject to editing.


When empty="anything" is set, $params['empty'] is always true. This is because smarty_function_validate() is checking if `true' is in the $params array (this will always evaluate to true unless $var == ""). Either enable the strict bool for in_array(), or just remove `true' from the list of comparisons.

Here's the diff:

[php:1:53ce4e7686]
*** SmartyValidate.class.php.orig Tue Jul 13 08:48:08 2004
--- SmartyValidate.class.php Thu Jul 22 16:18:35 2004
***************
*** 361,367 ****
* @param string $var the value being booleanized
*/
function _booleanize($var) {
! if(in_array(strtolower($var), array(true, 1, 'true','on','yes','y'))) {
return true;
}
return false;
--- 362,368 ----
* @param string $var the value being booleanized
*/
function _booleanize($var) {
! if(in_array(strtolower($var), array(true, 1, 'true','on','yes','y'),true)) {
return true;
}
return false;
[/php:1:53ce4e7686]


Secondly... either there needs to be an isset="yes" attribute, OR unset variables should not fail a validation if empty="yes" is true. From here, you could even reset the variable to NULL, or just leave it unset. Why? Checkboxes, for example, will not be sent if they are empty. Also, the validation should not execute when empty="yes":

Code:

    EMPTY
    -----
   
    {validate field="foo" ... empty="yes"}
   
    "empty" determines if the field is allowed to be empty or not. If
    allowed, the validation will be skipped when the field is empty.
    Note this is ignored with the "notEmpty" criteria.


This fix also rightly skips validation when empty="yes". And yet another diff:

[php:1:53ce4e7686]
*** SmartyValidate.class.php.orig Tue Jul 13 08:48:08 2004
--- SmartyValidate.class.php Fri Jul 23 14:18:23 2004
***************
*** 160,169 ****
$formvars[$_field] = substr($_field,5);
}

! if(!isset($formvars[$_field])) {
// field must exist, or else fails automatically
! $_sess[$_key]['valid'] = false;
! $_ret = false;
} else {
if(substr($_val['criteria'],0,1) == '@') {
// criteria will apply to entire array
--- 160,171 ----
$formvars[$_field] = substr($_field,5);
}

! if(!isset($formvars[$_field]) or
! (empty($formvars[$_field]) and $_empty)) {
// field must exist, or else fails automatically
! // UNLESS empty="yes"
! $_sess[$_key]['valid'] = $_empty;
! $_ret = (isset($_ret) and !$_ret)? false: $_empty;
} else {
if(substr($_val['criteria'],0,1) == '@') {
// criteria will apply to entire array
[/php:1:53ce4e7686]
Back to top
View user's profile Send private message
mohrt
Administrator


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

PostPosted: Fri Jul 23, 2004 9:33 pm    Post subject: Reply with quote

These changes are incorporated into CVS (with some cosmetic adjustments), please test and thanks for the patches!
Back to top
View user's profile Send private message Visit poster's website
B00zy
Smarty Rookie


Joined: 19 Jul 2004
Posts: 12

PostPosted: Sat Jul 24, 2004 5:22 pm    Post subject: Reply with quote

Code:

Notice: Array to string conversion in /web/sites/beano/Smarty-2.6.3/libs/SmartyValidate.class.php on line 166


I understand where you're going with this though: continue to validate on an actual zero.

[php:1:f924f6e796]
if(!isset($formvars[$_field])
|| ((is_array($formvars[$_field]) and empty($formvars[$_field]))
|| (!is_array($formvars[$_field]) and strlen((string)$formvars[$_field]) == 0)) && $_empty) {
[/php:1:f924f6e796]
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