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

SmartyValidate multi-level nested array support [PATCH]

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


Joined: 27 Feb 2009
Posts: 1

PostPosted: Fri Feb 27, 2009 12:16 am    Post subject: SmartyValidate multi-level nested array support [PATCH] Reply with quote

Add support for multi-level/nested array values such as foo[bar][baz], etc.

Code:

--- SmartyValidate.class.php.old   2007-04-23 10:50:34.000000000 -0400
+++ SmartyValidate.class.php   2009-02-26 19:06:36.000000000 -0500
@@ -153,6 +153,18 @@
                 }
                 $_field = substr($_field, 0, $_lpos);                   
             }
+
+         # Get nested value
+         if ($formvars[$_field]) $_cur = $formvars[$_field];
+         if (preg_match_all("/\[([^\]]+)\]+/", $_full_field, $matches)){
+            foreach ($matches[1] as $k){
+               if ($_cur[$k]) {
+                  $_nestedval = $_cur[$k];
+                  $_cur = $_cur[$k];
+               }
+            }
+         }
+
             
             if(isset($_sess[$_key]['transform'])) {
                 $_trans_names = preg_split('![\s,]+!', $_sess[$_key]['transform'], -1, PREG_SPLIT_NO_EMPTY);
@@ -185,7 +197,7 @@
                     if(is_array($formvars[$_field]) && !$_trans_on_array) {
                         if(isset($_field_key)) {
                             // only apply to given key
-                            if(($_new_val = SmartyValidate::_execute_transform($_trans_name, $formvars[$_field][$_field_key], $_sess[$_key], $formvars, $form)) !== false)
+                            if(($_new_val = SmartyValidate::_execute_transform($_trans_name, $_nestedval, $_sess[$_key], $formvars, $form)) !== false)
                                 $formvars[$_field][$_field_key] = $_new_val;
                             
                         } else {
@@ -222,7 +234,7 @@
                 if(is_array($formvars[$_field]) && !$_criteria_on_array) {
                     if(isset($_field_key)) {
                         // only apply to given key
-                        $_sess[$_key]['valid'] = SmartyValidate::_is_valid_criteria($_val['criteria'], $formvars[$_field][$_field_key], $_empty, $_sess[$_key], $formvars, $form);
+                        $_sess[$_key]['valid'] = SmartyValidate::_is_valid_criteria($_val['criteria'], $_nestedval, $_empty, $_sess[$_key], $formvars, $form);
                     } else {
                         // apply to all keys
                         foreach ($formvars[$_field] as $_fv_key => $_fv_value) {
[/code]
Back to top
View user's profile Send private message
Bilbo_UK
Smarty n00b


Joined: 16 Mar 2010
Posts: 2

PostPosted: Tue Mar 16, 2010 1:44 pm    Post subject: Reply with quote

Excellent! Many thanks for this - I was just about to do this job myself... Very Happy

Thanks,
Geoff
Back to top
View user's profile Send private message
Bilbo_UK
Smarty n00b


Joined: 16 Mar 2010
Posts: 2

PostPosted: Thu Mar 18, 2010 7:32 pm    Post subject: Reply with quote

Oops I take it back - this mod doesn't work.

"notEmpty" always fails... easy fix by changing
Code:
if ($_cur[$k]) {
to
Code:
if (isset($_cur[$k])) {

but applying a transform to a multi-level nested item messes up the form field array and all further validation is incorrect. Sad
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