function emptyarray($Val)
{
    if (empty($Val)) {
        return true;
    } elseif (is_array($Val)) {
        $empty = true;
        foreach ($Val as $Value) {
            if (!emptyarray($Value)) {
                $empty = false;
            }
        }
        return $empty;
    } else {
        return false;
    }
}
Example #2
0
 public function ValidateForm($FormName)
 {
     if (isset($this->main->SESSION['forms'][$FormName])) {
         $Valid = true;
         $Form =& $this->main->SESSION['forms'][$FormName];
         $FormEmpty = true;
         $FormNull = true;
         foreach ($Form as $Key => $Value) {
             $Function = 'form_param_' . $Key . '_validate';
             if (function_exists($Function)) {
                 $Return = $Function($this->main, $FormName);
                 if ($Return === false) {
                     $Valid = false;
                 }
             }
         }
         foreach ($Form['elements'] as $Key => $Element) {
             if (isset($Form['elements'][$Key]['errormsg'])) {
                 unset($Form['elements'][$Key]['errormsg']);
             }
             if ($Element['type'] == self::$FormElementType) {
                 $SubformName = $FormName . '_' . $Form['elements'][$Key]['id'];
                 $SubformResult = $this->ValidateForm($SubformName);
                 if ($SubformResult === false) {
                     $Errormsgs = array();
                     $Subform =& $this->main->SESSION['forms'][$SubformName];
                     foreach ($Subform['elements'] as $SubformKey => $SubformElement) {
                         if (isset($SubformElement['errormsg'])) {
                             foreach ($SubformElement['errormsg'] as $Errormsg) {
                                 $Errormsgs[] = array('name' => $Subform['name'] . ' ' . $SubformElement['errorname'], 'errormsg' => $Errormsg);
                             }
                         }
                     }
                     $Form['elements'][$Key]['errormsg'] = $Errormsgs;
                     $Valid = false;
                 }
             } elseif ($Element['type'] == self::$MultiFormElementType) {
                 RDD::Log('trigger multiform ' . $Key, TRACE, 1201);
                 $Count = $Element['count'];
                 $Errormsgs = array();
                 $MultiFormValid = true;
                 $ValidCount = 0;
                 $FalseCount = 0;
                 if ($Count > 0) {
                     $MultiSubformNameBase = $FormName . '_' . $Key;
                     for ($i = 0; $i < $Count; $i++) {
                         $MultiFormValues = array();
                         if (isset($Values[$Key]) && is_array($Values[$Key])) {
                             if (isset($Values[$Key][$i]) && is_array($Values[$Key][$i])) {
                                 $MultiFormValues = $Values[$Key][$i];
                             }
                         }
                         $MultiFormResult = $this->ValidateForm($MultiSubformNameBase . '_' . $i);
                         $MultiSubform =& $this->main->SESSION['forms'][$MultiSubformNameBase . '_' . $i];
                         if (isset($Element['ignoreempty'])) {
                             if ($MultiSubform['formempty']) {
                                 $MultiFormResult = 'empty';
                             }
                         } elseif (isset($Element['ignorenull'])) {
                             if ($MultiSubform['formnull']) {
                                 $MultiFormResult = 'null';
                             }
                         }
                         if (isset($Element['minimum'])) {
                             if ($MultiFormResult !== false && $MultiFormResult !== 'empty' && $MultiFormResult !== 'null') {
                                 $ValidCount++;
                             }
                             if ($MultiFormResult === false) {
                                 $FalseCount++;
                             }
                         }
                         if ($MultiFormResult === false || $MultiFormResult === 'empty' || $MultiFormResult === 'null') {
                             if (isset($Element['geterrors'])) {
                                 foreach ($MultiSubform['elements'] as $SubformKey => $SubformElement) {
                                     if (isset($SubformElement['errormsg'])) {
                                         foreach ($SubformElement['errormsg'] as $Errormsg) {
                                             $Number = $i + 1;
                                             $Name = isset($Element['name']) ? $Element['name'] : $MultiSubform['name'];
                                             $Errormsgs[] = array('name' => $Number . '. ' . $Name . ' ' . $SubformElement['errorname'], 'errormsg' => $Errormsg);
                                         }
                                     }
                                 }
                             }
                             $MultiFormValid = false;
                         }
                     }
                 }
                 if (isset($Element['minimum']) && !$FalseCount) {
                     if ($ValidCount >= $Element['minimum']) {
                         $MultiFormValid = true;
                         unset($Errormsgs);
                     } else {
                         $Errormsgs[] = T($Element['minimumerrormsg'], array($Element['minimum']));
                     }
                 }
                 if ($MultiFormValid === false) {
                     $Valid = false;
                     $Form['elements'][$Key]['errormsg'] = $Errormsgs;
                 }
             } else {
                 if (emptyarray($Element['value']) && $Form['elements'][$Key]['value'] !== 0 && $Form['elements'][$Key]['value'] !== "0" && $Form['elements'][$Key]['value'] !== NULL) {
                     $Empty = true;
                 } else {
                     $Empty = false;
                     $FormEmpty = false;
                 }
                 if (empty($Element['value'])) {
                     $Null = true;
                 } else {
                     $Null = false;
                     $FormNull = false;
                 }
                 $ElementValid = true;
                 /**
                  * Using validators for checking form data
                  */
                 if (isset($Element['validators'])) {
                     /**
                      * Checking for notempty validator
                      */
                     $defaulterrormsg = false;
                     $form_defaulterrormsg = false;
                     $element_defaulterrormsg = false;
                     $element_onlyerrormsg = false;
                     foreach ($Element['validators'] as $ValidatorKey => $Validator) {
                         if ($Validator['type'] == self::$NotEmptyType) {
                             if ($Empty) {
                                 $Return = empty($Validator['error']) ? self::$EmptyErrorMessage : $Validator['error'];
                             } else {
                                 $Return = true;
                             }
                         } elseif ($Validator['type'] == self::$NotNullType) {
                             if ($Null) {
                                 $Return = self::$NullErrorMessage;
                             } else {
                                 $Return = true;
                             }
                         } elseif ($Empty) {
                             $Return = true;
                         } else {
                             #
                             #
                             # Validator
                             #
                             ############
                             $Validator['form'] = $Form;
                             $Validator['formname'] = $FormName;
                             $Validator['element'] = $Key;
                             $Return = $this->Validate($Validator['type'], $Form['elements'][$Key]['value'], $Validator);
                         }
                         if ($Return === false || is_string($Return) || is_a($Return, 'RDT')) {
                             $Valid = false;
                             $ElementValid = false;
                             if (!isset($Form['elements'][$Key]['errormsg']) || !is_array($Form['elements'][$Key]['errormsg'])) {
                                 $Form['elements'][$Key]['errormsg'] = array();
                             }
                             if (isset($Validator['errormsg'])) {
                                 $Form['elements'][$Key]['errormsg'][] = T($Validator['errormsg']);
                             } elseif (isset($Form['elements'][$Key]['onlyerrormsg'])) {
                                 if (!$element_onlyerrormsg) {
                                     $Form['elements'][$Key]['errormsg'][] = T($Form['elements'][$Key]['onlyerrormsg']);
                                     $element_onlyerrormsg = true;
                                 }
                             } elseif (is_string($Return)) {
                                 $Form['elements'][$Key]['errormsg'][] = T($Return);
                             } elseif (is_a($Return, 'RDT')) {
                                 $Form['elements'][$Key]['errormsg'][] = $Return;
                             } else {
                                 if (isset($Form['elements'][$Key]['defaulterrormsg'])) {
                                     if (!$element_defaulterrormsg) {
                                         $Form['elements'][$Key]['errormsg'][] = T($Form['elements'][$Key]['defaulterrormsg']);
                                         $element_defaulterrormsg = true;
                                     }
                                 } elseif (isset($Form['defaulterrormsg'])) {
                                     if (!$form_defaulterrormsg) {
                                         $Form['elements'][$Key]['errormsg'][] = T($Form['defaulterrormsg']);
                                         $form_defaulterrormsg = true;
                                     }
                                 } else {
                                     if (!$defaulterrormsg) {
                                         $Form['elements'][$Key]['errormsg'][] = self::$DefaultErrorMessage;
                                         $defaulterrormsg = true;
                                     }
                                 }
                             }
                         }
                     }
                 }
                 $validate_function = 'form_type_' . $Element['type'] . '_validate';
                 if (function_exists($validate_function)) {
                     $Form['elements'][$Key] = $validate_function($Form['elements'][$Key]);
                     if (isset($Form['elements'][$Key]['valid']) && $Form['elements'][$Key]['valid'] === false) {
                         $Valid = false;
                         $ElementValid = false;
                     }
                 }
                 $Form['elements'][$Key]['valid'] = $ElementValid;
             }
         }
         $this->main->SESSION['forms'][$FormName]['formempty'] = $FormEmpty;
         $this->main->SESSION['forms'][$FormName]['formnull'] = $FormNull;
         if ($Valid) {
             $Form['valid'] = true;
             foreach ($Form['elements'] as $Key => $Element) {
                 if ($Element['type'] == self::$FormElementType) {
                     $SubformName = $FormName . '_' . $Key;
                     $FinalValue = $this->FinalValuesForm($SubformName);
                 } elseif ($Element['type'] == self::$MultiFormElementType) {
                     $Count = $Element['count'];
                     if ($Count > 0) {
                         $MultiSubformNameBase = $FormName . '_' . $Key;
                         $MultiformResult = array();
                         for ($i = 0; $i < $Count; $i++) {
                             $MultiFormResult = $this->FinalValuesForm($MultiSubformNameBase . '_' . $i);
                             if (!empty($MultiFormResult)) {
                                 $MultiformResult[$i] = $MultiFormResult;
                             }
                         }
                     }
                     $FinalValue = $MultiformResult;
                 } else {
                     $FinalValue = $Form['elements'][$Key]['value'];
                 }
                 #
                 # Transform
                 #
                 ###############
                 $Transforms = array();
                 if (isset($Element['transforms'])) {
                     foreach ($Element['transforms'] as $Transform) {
                         $Transforms[] = $Transform['type'];
                     }
                 }
                 if (isset($this->Config['DefaultTransforms'][$Element['type']])) {
                     foreach ($this->Config['DefaultTransforms'][$Element['type']] as $Transform) {
                         if (!in_array($Transform, $Transforms)) {
                             $Transforms[] = $Transform;
                         }
                     }
                 }
                 foreach ($Transforms as $Transform) {
                     $function = 'form_transform_' . $Transform;
                     if (function_exists($function)) {
                         $FinalValue = $function($this->main, $Form, $Element, $FinalValue);
                     } elseif (function_exists($Transform)) {
                         $FinalValue = $Transform($FinalValue);
                     } else {
                         throw new RDE('RD_Form: cant find transform method ' . $Transform);
                     }
                 }
                 $Form['elements'][$Key]['finalvalue'] = $FinalValue;
             }
             if (isset($this->main->SESSION['forms'][$FormName]['error'])) {
                 unset($this->main->SESSION['forms'][$FormName]['error']);
             }
         } else {
             $Form['valid'] = false;
         }
         foreach ($Form['elements'] as $Key => $Element) {
             $post_validate_function = 'form_type_' . $Element['type'] . '_post_validate';
             if (function_exists($post_validate_function)) {
                 $Form['elements'][$Key] = $post_validate_function($Form['elements'][$Key], $Valid);
             }
         }
         return $Valid;
     } else {
         RDD::Log('form ' . $FormName . ' not loaded', WARN);
         return false;
     }
 }