示例#1
0
 /**
  * Validate Custom Form Fields
  * @param Validation $post Validation object from form post
  * XXX This whole function is being done backwards
  * Need to pull the list of custom form fields first
  * Then look through them to see if they're set, not the other way around.
  */
 public static function validate_custom_form_fields(&$post)
 {
     $custom_fields = array();
     if (!isset($post->custom_field)) {
         return;
     }
     /* XXX Checkboxes hackery
     			 Checkboxes are submitted in the post as custom_field[field_id-boxnum]
     			 This foreach loop consolidates them into one variable separated by commas.
     			 If no checkboxes are selected then the custom_field[] for that variable is not sent
     			 To get around that the view sets a hidden custom_field[field_id-BLANKHACK] field that
     			 ensures the checkbox custom_field is there to be tested.
     		*/
     foreach ($post->custom_field as $field_id => $field_response) {
         $split = explode("-", $field_id);
         if (isset($split[1])) {
             // The view sets a hidden field for blankhack
             if ($split[1] == 'BLANKHACK') {
                 if (!isset($custom_fields[$split[0]])) {
                     // then no checkboxes were checked
                     $custom_fields[$split[0]] = '';
                 }
                 // E.Kala - Removed the else {} block; either way continue is still invoked
                 continue;
             }
             if (isset($custom_fields[$split[0]])) {
                 $custom_fields[$split[0]] .= ",{$field_response}";
             } else {
                 $custom_fields[$split[0]] = $field_response;
             }
         } else {
             $custom_fields[$split[0]] = $field_response;
         }
     }
     $post->custom_field = $custom_fields;
     // Kohana::log('debug', Kohana::debug($custom_fields));
     foreach ($post->custom_field as $field_id => $field_response) {
         $field_param = ORM::factory('form_field', $field_id);
         $custom_name = $field_param->field_name;
         // Validate that this custom field already exists
         if (!$field_param->loaded) {
             // Populate the error field
             //$errors[$field_id] = "The $custom_name field does not exist";
             $post->add_error('custom_field', 'not_exist', array($field_id));
             return;
         }
         $max_auth = self::get_user_max_auth();
         $required_role = ORM::factory('role', $field_param->field_ispublic_submit);
         if (($required_role->loaded ? $required_role->access_level : 0) > $max_auth) {
             // Populate the error field
             $post->add_error('custom_field', 'permission', array($custom_name));
             return;
         }
         // Validate that the field is required
         if ($field_param->field_required == 1 and $field_response == "") {
             $post->add_error('custom_field', 'required', array($custom_name));
             return;
         }
         // Grab the custom field options for this field
         $field_options = self::get_custom_field_options($field_id);
         // Validate Custom fields for text boxes
         if ($field_param->field_type == 1 and isset($field_options) and $field_response != '') {
             if (isset($field_options['field_datatype'])) {
                 if ($field_options['field_datatype'] == 'email' and !valid::email($field_response)) {
                     $post->add_error('custom_field', 'email', array($custom_name));
                 }
                 if ($field_options['field_datatype'] == 'phonenumber' and !valid::phone($field_response)) {
                     $post->add_error('custom_field', 'phone', array($custom_name));
                 }
                 if ($field_options['field_datatype'] == 'numeric' and !valid::numeric($field_response)) {
                     $post->add_error('custom_field', 'numeric', array($custom_name));
                 }
             }
         }
         // Validate for date
         if ($field_param->field_type == 3 and $field_response != "") {
             $field_default = $field_param->field_default;
             if (!valid::date_mmddyyyy($field_response)) {
                 $post->add_error('custom_field', 'date_mmddyyyy', array($custom_name));
             }
         }
         // Validate multi-value boxes only have acceptable values
         if ($field_param->field_type >= 5 and $field_param->field_type <= 7) {
             $defaults = explode('::', $field_param->field_default);
             $options = array();
             if (preg_match("/[0-9]+-[0-9]+/", $defaults[0]) and count($defaults) == 1) {
                 $dashsplit = explode('-', $defaults[0]);
                 $start = $dashsplit[0];
                 $end = $dashsplit[1];
                 for ($i = $start; $i <= $end; $i++) {
                     array_push($options, $i);
                 }
             } else {
                 $options = array_map('trim', explode(',', $defaults[0]));
             }
             $responses = explode(',', $field_response);
             foreach ($responses as $response) {
                 if (!in_array($response, $options) and $response != '') {
                     $post->add_error('custom_field', 'values', array($custom_name));
                     //$errors[$field_id] = "The $custom_name field does not include $response as an option";
                 }
             }
         }
         // Validate that a required checkbox is checked
         if ($field_param->field_type == 6 and $field_response == 'BLANKHACK' and $field_param->field_required == 1) {
             $post->add_error('custom_field', 'required', array($custom_name));
         }
     }
     return;
 }