예제 #1
0
 /**
  * 
  * Adds element rules to a pre-existing HTML_QuickForm object.
  * 
  * @static
  * 
  * @access public
  * 
  * @param object &$form An HTML_QuickForm object.
  * 
  * @param array $cols A sequential array of DB_Table column definitions
  * from which to create form elements.
  * 
  * @param string $arrayName By default, the form will use the names
  * of the columns as the names of the form elements.  If you pass
  * $arrayName, the column names will become keys in an array named
  * for this parameter.
  * 
  * @param string $clientValidate By default, validation will match
  * the 'qf_client' value from the column definition.  However,
  * if you set $clientValidate to true or false, this will
  * override the value from the column definition.
  * 
  * @return void
  * 
  */
 function addRules(&$form, $cols, $arrayName = null, $clientValidate = null)
 {
     foreach ($cols as $name => $col) {
         if ($arrayName) {
             $elemname = $arrayName . "[{$name}]";
         } else {
             $elemname = $name;
         }
         // make sure all necessary elements are in place
         DB_Table_QuickForm::fixColDef($col, $elemname);
         // if clientValidate is specified, override the column
         // definition.  otherwise use the col def as it is.
         if (!is_null($clientValidate)) {
             // override
             if ($clientValidate) {
                 $validate = 'client';
             } else {
                 $validate = 'server';
             }
         } else {
             // use as-is
             if ($col['qf_client']) {
                 $validate = 'client';
             } else {
                 $validate = 'server';
             }
         }
         // **always** override these rules to make them
         // server-side only.  suggested by Mark Wiesemann,
         // debugged by Hero Wanders.
         $onlyServer = array('filename', 'maxfilesize', 'mimetype', 'uploadedfile');
         // loop through the rules and add them
         foreach ($col['qf_rules'] as $type => $opts) {
             // some rules (e.g. rules for file elements) can only be
             // checked on the server; therefore, don't use client-side
             // validation for these rules
             $ruleValidate = $validate;
             if (in_array($type, $onlyServer)) {
                 $ruleValidate = 'server';
             }
             switch ($type) {
                 case 'alphanumeric':
                 case 'email':
                 case 'lettersonly':
                 case 'nonzero':
                 case 'nopunctuation':
                 case 'numeric':
                 case 'required':
                 case 'uploadedfile':
                     // $opts is the error message
                     $message = $opts;
                     $format = null;
                     break;
                 case 'filename':
                 case 'maxfilesize':
                 case 'maxlength':
                 case 'mimetype':
                 case 'minlength':
                 case 'regex':
                     // $opts[0] is the message
                     // $opts[1] is the size, mimetype, or regex
                     $message = $opts[0];
                     $format = $opts[1];
                     break;
                 default:
                     // by Alex Hoebart: this should allow any registered rule.
                     if (!in_array($type, $form->getRegisteredRules())) {
                         // rule is not registered ==> do not add a rule
                         continue;
                     }
                     if (is_array($opts)) {
                         // $opts[0] is the message
                         // $opts[1] is the size or regex
                         $message = $opts[0];
                         $format = $opts[1];
                     } else {
                         // $opts is the error message
                         $message = $opts;
                         $format = null;
                     }
                     break;
             }
             switch ($col['qf_type']) {
                 case 'date':
                 case 'time':
                 case 'timestamp':
                     // date "elements" are groups ==> use addGroupRule()
                     $form->addGroupRule($elemname, $message, $type, $format, null, $ruleValidate);
                     break;
                 default:
                     // use addRule() for all other elements
                     $form->addRule($elemname, $message, $type, $format, $ruleValidate);
                     break;
             }
         }
     }
 }
예제 #2
0
 /**
  * 
  * Creates and returns an array of QuickForm elements based on a DB_Table
  * column name.
  * 
  * @author Ian Eure <*****@*****.**>
  * 
  * @access public
  * 
  * @param string $cols Array of DB_Table column names
  * 
  * @param string $array_name The name to use for the generated QuickForm
  * elements.
  * 
  * @return object HTML_QuickForm_Element
  * 
  * @see HTML_QuickForm
  * 
  * @see DB_Table_QuickForm
  * 
  */
 function &getFormElements($cols, $array_name = null)
 {
     include_once 'DB/Table/QuickForm.php';
     $elements =& DB_Table_QuickForm::getElements($cols, $array_name);
     return $elements;
 }
예제 #3
0
 /**
  * 
  * Adds element rules to a pre-existing HTML_QuickForm object.
  * 
  * @static
  * 
  * @access public
  * 
  * @param object &$form An HTML_QuickForm object.
  * 
  * @param array $cols A sequential array of DB_Table column definitions
  * from which to create form elements.
  * 
  * @param string $arrayName By default, the form will use the names
  * of the columns as the names of the form elements.  If you pass
  * $arrayName, the column names will become keys in an array named
  * for this parameter.
  * 
  * @param string $clientValidate By default, validation will match
  * the 'qf_client' value from the column definition.  However,
  * if you set $clientValidate to true or false, this will
  * override the value from the column definition.
  * 
  * @return void
  * 
  */
 function addRules(&$form, $cols, $arrayName = null, $clientValidate = null)
 {
     foreach ($cols as $name => $col) {
         if ($arrayName) {
             $elemname = $arrayName . "[{$name}]";
         } else {
             $elemname = $name;
         }
         // make sure all necessary elements are in place
         DB_Table_QuickForm::fixColDef($col, $elemname);
         // if clientValidate is specified, override the column
         // definition.  otherwise use the col def as it is.
         if (!is_null($clientValidate)) {
             // override
             if ($clientValidate) {
                 $validate = 'client';
             } else {
                 $validate = 'server';
             }
         } else {
             // use as-is
             if ($col['qf_client']) {
                 $validate = 'client';
             } else {
                 $validate = 'server';
             }
         }
         // **always** override these rules to make them
         // server-side only.  suggested by Mark Wiesemann,
         // debugged by Hero Wanders.
         $onlyServer = array('filename', 'maxfilesize', 'mimetype', 'uploadedfile');
         // loop through the rules and add them
         foreach ($col['qf_rules'] as $type => $opts) {
             // override the onlyServer types so that we don't attempt
             // client-side validation at all.
             if (in_array($type, $onlyServer)) {
                 $validate = 'server';
             }
             switch ($type) {
                 case 'alphanumeric':
                 case 'email':
                 case 'lettersonly':
                 case 'nonzero':
                 case 'nopunctuation':
                 case 'numeric':
                 case 'required':
                 case 'uploadedfile':
                     // $opts is the error message
                     $form->addRule($elemname, $opts, $type, null, $validate);
                     break;
                 case 'filename':
                 case 'maxfilesize':
                 case 'maxlength':
                 case 'mimetype':
                 case 'minlength':
                 case 'regex':
                     // $opts[0] is the message
                     // $opts[1] is the size, mimetype, or regex
                     $form->addRule($elemname, $opts[0], $type, $opts[1], $validate);
                     break;
                 default:
                     // by Alex Hoebart: this should allow any registered rule.
                     if (in_array($type, $form->getRegisteredRules())) {
                         if (is_array($opts)) {
                             // $opts[0] is the message, $opts[1] is the size or regex
                             $form->addRule($elemname, $opts[0], $type, $opts[1], $validate);
                         } else {
                             // $opts is the error message
                             $form->addRule($elemname, $opts, $type, $validate);
                         }
                     }
                     break;
             }
         }
     }
 }