Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added an extra parameter to Form::Verify that allows you to run custom

verifications on the whole form (not bound to any specific form field)
  • Loading branch information...
commit f4f406cad5caa0e8a1b047c24915e1c0ce965184 1 parent 598616b
cless authored December 14, 2011

Showing 1 changed file with 68 additions and 1 deletion. Show diff stats Hide diff stats

  1. 69  htdocs/library/form.php
69  htdocs/library/form.php
@@ -295,10 +295,24 @@ public function VerifyField($name)
295 295
         /**
296 296
          * Verify all fields known to the form and set the error values (if any). You can call
297 297
          * Form::GetErrors after this function te get the actual error values.
  298
+         * 
  299
+         * \param $functions if this parameter is set to false it is ignored. Otherwise it should be
  300
+         *                   the name of a function, or an array of function names. Function names
  301
+         *                   are strings of the form 'functionname' or 'classname::staticfunction'
  302
+         *                   The function takes one argument: an array of values described in the form (see Form::GetValues())
  303
+         *                   The function can return true to indicate verification is successful, or false if verification
  304
+         *                   failed. Additionally the function can return an array of key=>value pairs where key is the name 
  305
+         *                   of the field that caused verification to fail and value can be either a custom error, or false
  306
+         *                   if you want to trigger the default error as described in the form field (see Form::AddField()).
  307
+         *                   The key does not have to be the name of a described form field, it can also be a custom name. In
  308
+         *                   that case value has to be an  error string and can not be false.
  309
+         *                   All these functions are executed in the order they are returned by foreach($functions_array). The
  310
+         *                   functions are always executed, regardless of whether the verification already failed or not.
298 311
          * \return true when all fields pass verification, false otherwise.
299 312
          */
300  
-        public function Verify()
  313
+        public function Verify($functions = false)
301 314
         {
  315
+
302 316
             $verdict = true;
303 317
             $this->errors = array();
304 318
             foreach ($this->fields as $name => $field)
@@ -309,9 +323,62 @@ public function Verify()
309 323
                     $this->errors[$name] = $field['error'];
310 324
                 }
311 325
             }
  326
+
  327
+            if (is_string($functions))
  328
+            {
  329
+                $arg = $this->GetValues();
  330
+                $ret = $this->CallCustomVerification($functions, $arg);
  331
+                if($ret === false)
  332
+                    $verdict = false;
  333
+                else if(is_array($ret))
  334
+                {
  335
+                    $verdict = false;
  336
+                    $this->ParseCustomErrors($ret);
  337
+                }
  338
+            }
  339
+            elseif (is_array($functions))
  340
+            {
  341
+                $arg = $this->GetValues();
  342
+
  343
+                foreach ($functions as $function)
  344
+                {
  345
+                    $ret = $this->CallCustomVerification($function, $arg);
  346
+                    if($ret === false)
  347
+                        $verdict = false;
  348
+                    else if(is_array($ret))
  349
+                    {
  350
+                        $verdict = false;
  351
+                        $this->ParseCustomErrors($ret);
  352
+                    }
  353
+                }
  354
+            }
  355
+
312 356
             return $verdict;
313 357
         }
314 358
 
  359
+        // Helper function for ^ Form::Verify
  360
+        private function CallCustomVerification($name, $arg)
  361
+        {
  362
+                $parts = explode(':', $name);
  363
+
  364
+                if(count($parts) == 3)
  365
+                    return $parts[0]::$parts[2]($arg);
  366
+                
  367
+                return $name($arg);
  368
+        }
  369
+
  370
+        // Helper function for ^ Form::Verify
  371
+        private function ParseCustomErrors($errors)
  372
+        {
  373
+            foreach ($errors as $name => $error)
  374
+            {
  375
+                if($error == false)
  376
+                    $this->errors[$name] = $this->fields[$name]['error'];
  377
+                else
  378
+                    $this->errors[$name] = $error;
  379
+            }
  380
+        }
  381
+
315 382
         /**
316 383
          * Fetch the submitted values (if any) of all fields described with Form::AddField
317 384
          * \return Returns an array of key=>value pairs where key is the form field name and value is the contents.

0 notes on commit f4f406c

Please sign in to comment.
Something went wrong with that request. Please try again.