/**
  * Make validation work for Edit Entry
  *
  * Because we're calling the GFFormDisplay::validate() in an unusual way (as a front-end
  * form pretending to be a back-end form), validate() doesn't know we _can't_ edit post
  * fields. This goes through all the fields and if they're an invalid post field, we
  * set them as valid. If there are still issues, we'll return false.
  *
  * @param  [type] $validation_results [description]
  * @return [type]                     [description]
  */
 function custom_validation($validation_results)
 {
     do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Validation results: ', $validation_results);
     do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] $_POSTed data (sanitized): ', esc_html(print_r($_POST, true)));
     $gv_valid = true;
     foreach ($validation_results['form']['fields'] as $key => &$field) {
         $value = RGFormsModel::get_field_value($field);
         $field_type = RGFormsModel::get_input_type($field);
         // Validate always
         switch ($field_type) {
             case 'fileupload':
                 // in case nothing is uploaded but there are already files saved
                 if (!empty($field->failed_validation) && !empty($field->isRequired) && !empty($value)) {
                     $field->failed_validation = false;
                     unset($field->validation_message);
                 }
                 // validate if multi file upload reached max number of files [maxFiles] => 2
                 if (rgar($field, 'maxFiles') && rgar($field, 'multipleFiles')) {
                     $input_name = 'input_' . $field->id;
                     //uploaded
                     $file_names = isset(GFFormsModel::$uploaded_files[$validation_results['form']['id']][$input_name]) ? GFFormsModel::$uploaded_files[$validation_results['form']['id']][$input_name] : array();
                     //existent
                     $entry = $this->get_entry();
                     $value = NULL;
                     if (isset($entry[$field->id])) {
                         $value = json_decode($entry[$field->id], true);
                     }
                     // count uploaded files and existent entry files
                     $count_files = count($file_names) + count($value);
                     if ($count_files > $field->maxFiles) {
                         $field->validation_message = __('Maximum number of files reached', 'gravityview');
                         $field->failed_validation = 1;
                         $gv_valid = false;
                         // in case of error make sure the newest upload files are removed from the upload input
                         GFFormsModel::$uploaded_files[$validation_results['form']['id']] = null;
                     }
                 }
                 break;
         }
         // This field has failed validation.
         if (!empty($field->failed_validation)) {
             do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Field is invalid.', array('field' => $field, 'value' => $value));
             switch ($field_type) {
                 // Captchas don't need to be re-entered.
                 case 'captcha':
                     // Post Image fields aren't editable, so we un-fail them.
                 // Post Image fields aren't editable, so we un-fail them.
                 case 'post_image':
                     $field->failed_validation = false;
                     unset($field->validation_message);
                     break;
             }
             // You can't continue inside a switch, so we do it after.
             if (empty($field->failed_validation)) {
                 continue;
             }
             // checks if the No Duplicates option is not validating entry against itself, since
             // we're editing a stored entry, it would also assume it's a duplicate.
             if (!empty($field->noDuplicates)) {
                 $entry = $this->get_entry();
                 // If the value of the entry is the same as the stored value
                 // Then we can assume it's not a duplicate, it's the same.
                 if (!empty($entry) && $value == $entry[$field->id]) {
                     //if value submitted was not changed, then don't validate
                     $field->failed_validation = false;
                     unset($field->validation_message);
                     do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Field not a duplicate; it is the same entry.', $entry);
                     continue;
                 }
             }
             // if here then probably we are facing the validation 'At least one field must be filled out'
             if (GFFormDisplay::is_empty($field, $this->form_id) && empty($field->isRequired)) {
                 unset($field->validation_message);
                 $field->validation_message = false;
                 continue;
             }
             $gv_valid = false;
         }
     }
     $validation_results['is_valid'] = $gv_valid;
     do_action('gravityview_log_debug', 'GravityView_Edit_Entry[custom_validation] Validation results.', $validation_results);
     // We'll need this result when rendering the form ( on GFFormDisplay::get_form )
     $this->form_after_validation = $validation_results['form'];
     return $validation_results;
 }
 private function validate(&$form, $field_values)
 {
     $form = apply_filters('gform_pre_validation', $form);
     foreach ($form["fields"] as &$field) {
         /*
          * Skip over the following fields as we aren't processing any of them
          */
         $skip_field = false;
         switch (RGFormsModel::get_input_type($field)) {
             case "captcha":
             case "html":
             case "password":
             case "product":
             case "coupon":
             case "quantity":
             case "shipping":
             case "donation":
             case "total":
             case "singleproduct":
             case "hiddenproduct":
             case "singleshipping":
             case "creditcard":
             case "page":
             case "post_image":
             case "fileupload":
                 //ignore certain fields
                 $skip_field = true;
                 break;
         }
         if (isset($field['productField']) && (int) $field['productField'] > 0 || $field['type'] == 'shipping') {
             $skip_field = true;
         }
         /* ignore validation if field is hidden or admin only */
         if (RGFormsModel::is_field_hidden($form, $field, $field_values) || isset($field['adminOnly']) && $field['adminOnly']) {
             $skip_field = true;
         }
         /* ignore user-defined restricted fields or hidden fields */
         if (in_array($field['id'], $this->atts['restricted_fields']) || in_array($field['id'], $this->atts['hidden_fields'])) {
             $skip_field = true;
         }
         if ($skip_field) {
             continue;
         }
         $value = RGFormsModel::get_field_value($field);
         //display error message if field is marked as required and the submitted value is empty
         if ($field["isRequired"] && GFFormDisplay::is_empty($field, $form["id"])) {
             $field["failed_validation"] = true;
             $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required.", "gravityforms") : $field["errorMessage"];
         } else {
             if ($field["noDuplicates"] && RGFormsModel::is_duplicate($form["id"], $field, $value)) {
                 $field["failed_validation"] = true;
                 $input_type = RGFormsModel::get_input_type($field);
                 switch ($input_type) {
                     case "date":
                         $default_message = __("This date has already been taken. Please select a new date.", "gravityforms");
                         break;
                     default:
                         $default_message = is_array($value) ? __("This field requires a unique entry and the values you entered have been already been used.", "gravityforms") : sprintf(__("This field requires a unique entry and '%s' has already been used", "gravityforms"), $value);
                         break;
                 }
                 $field["validation_message"] = apply_filters("gform_duplicate_message_{$form["id"]}", apply_filters("gform_duplicate_message", $default_message, $form, $field, $value), $form, $field, $value);
             } else {
                 if (GFFormDisplay::failed_state_validation($form["id"], $field, $value)) {
                     $field["failed_validation"] = true;
                     $field["validation_message"] = in_array($field["inputType"], array("singleproduct", "singleshipping", "hiddenproduct")) ? __("Please enter a valid value.", "gravityforms") : __("Invalid selection. Please select one of the available choices.", "gravityforms");
                 } else {
                     switch (RGFormsModel::get_input_type($field)) {
                         case "name":
                             if ($field["isRequired"] && $field["nameFormat"] != "simple") {
                                 $first = $_POST["input_" . $field["id"] . "_3"];
                                 $last = $_POST["input_" . $field["id"] . "_6"];
                                 if (empty($first) || empty($last)) {
                                     $field["failed_validation"] = true;
                                     $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required. Please enter the first and last name.", "gravityforms") : $field["errorMessage"];
                                 }
                             }
                             break;
                         case "address":
                             if ($field["isRequired"]) {
                                 $street = $_POST["input_" . $field["id"] . "_1"];
                                 $city = $_POST["input_" . $field["id"] . "_3"];
                                 $state = $_POST["input_" . $field["id"] . "_4"];
                                 $zip = $_POST["input_" . $field["id"] . "_5"];
                                 $country = $_POST["input_" . $field["id"] . "_6"];
                                 if (empty($street) || empty($city) || empty($zip) || empty($state) && !$field["hideState"] || empty($country) && !$field["hideCountry"]) {
                                     $field["failed_validation"] = true;
                                     $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required. Please enter a complete address.", "gravityforms") : $field["errorMessage"];
                                 }
                             }
                             break;
                         case "email":
                             if (!rgblank($value) && !GFCommon::is_valid_email($value)) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = empty($field["errorMessage"]) ? __("Please enter a valid email address.", "gravityforms") : $field["errorMessage"];
                             } else {
                                 if (rgget("emailConfirmEnabled", $field) && !empty($value)) {
                                     $confirm = rgpost("input_" . $field["id"] . "_2");
                                     if ($confirm != $value) {
                                         $field["failed_validation"] = true;
                                         $field["validation_message"] = __("Your emails do not match.", "gravityforms");
                                     }
                                 }
                             }
                             break;
                         case "price":
                             if (!class_exists("RGCurrency")) {
                                 require_once "currency.php";
                             }
                             $donation = GFCommon::to_number($value);
                             if (!rgblank($value) && ($donation === false || $donation < 0)) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = empty($field["errorMessage"]) ? __("Please enter a valid amount.", "gravityforms") : $field["errorMessage"];
                             }
                             break;
                         case "number":
                             // the POST value has already been converted from currency or decimal_comma to decimal_dot and then cleaned in get_field_value()
                             $value = GFCommon::maybe_add_leading_zero($value);
                             $raw_value = $_POST["input_" . $field["id"]];
                             //Raw value will be tested against the is_numeric() function to make sure it is in the right format.
                             $requires_valid_number = !rgblank($raw_value) && !GFCommon::has_field_calculation($field);
                             $is_valid_number = self::validate_range($field, $value) && GFCommon::is_numeric($raw_value, $field["numberFormat"]);
                             if ($requires_valid_number && !$is_valid_number) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = empty($field["errorMessage"]) ? GFCommon::get_range_message($field) : $field["errorMessage"];
                             } else {
                                 if ($field['type'] == 'quantity') {
                                     if (intval($value) != $value) {
                                         $field['failed_validation'] = true;
                                         $field['validation_message'] = empty($field['errorMessage']) ? __('Please enter a valid quantity. Quantity cannot contain decimals.', 'gravityforms') : $field['errorMessage'];
                                     } else {
                                         if (!empty($value) && (!is_numeric($value) || intval($value) != floatval($value) || intval($value) < 0)) {
                                             $field['failed_validation'] = true;
                                             $field['validation_message'] = empty($field['errorMessage']) ? __('Please enter a valid quantity', 'gravityforms') : $field['errorMessage'];
                                         }
                                     }
                                 }
                             }
                             break;
                         case "phone":
                             $regex = '/^\\D?(\\d{3})\\D?\\D?(\\d{3})\\D?(\\d{4})$/';
                             if ($field["phoneFormat"] == "standard" && $value !== "" && $value !== 0 && !preg_match($regex, $value)) {
                                 $field["failed_validation"] = true;
                                 if (!empty($field["errorMessage"])) {
                                     $field["validation_message"] = $field["errorMessage"];
                                 }
                             }
                             break;
                         case "date":
                             if (is_array($value) && rgempty(0, $value) && rgempty(1, $value) && rgempty(2, $value)) {
                                 $value = null;
                             }
                             if (!empty($value)) {
                                 $format = empty($field["dateFormat"]) ? "mdy" : $field["dateFormat"];
                                 $date = GFCommon::parse_date($value, $format);
                                 if (empty($date) || !GFFormDisplay::checkdate($date["month"], $date["day"], $date["year"])) {
                                     $field["failed_validation"] = true;
                                     $format_name = "";
                                     switch ($format) {
                                         case "mdy":
                                             $format_name = "mm/dd/yyyy";
                                             break;
                                         case "dmy":
                                             $format_name = "dd/mm/yyyy";
                                             break;
                                         case "dmy_dash":
                                             $format_name = "dd-mm-yyyy";
                                             break;
                                         case "dmy_dot":
                                             $format_name = "dd.mm.yyyy";
                                             break;
                                         case "ymd_slash":
                                             $format_name = "yyyy/mm/dd";
                                             break;
                                         case "ymd_dash":
                                             $format_name = "yyyy-mm-dd";
                                             break;
                                         case "ymd_dot":
                                             $format_name = "yyyy.mm.dd";
                                             break;
                                     }
                                     $message = $field["dateType"] == "datepicker" ? sprintf(__("Please enter a valid date in the format (%s).", "gravityforms"), $format_name) : __("Please enter a valid date.", "gravityforms");
                                     $field["validation_message"] = empty($field["errorMessage"]) ? $message : $field["errorMessage"];
                                 }
                             }
                             break;
                         case "time":
                             //create variable values if time came in one field
                             if (!is_array($value) && !empty($value)) {
                                 preg_match('/^(\\d*):(\\d*) ?(.*)$/', $value, $matches);
                                 $value = array();
                                 $value[0] = $matches[1];
                                 $value[1] = $matches[2];
                             }
                             $hour = $value[0];
                             $minute = $value[1];
                             if (empty($hour) && empty($minute)) {
                                 break;
                             }
                             $is_valid_format = is_numeric($hour) && is_numeric($minute);
                             $min_hour = rgar($field, "timeFormat") == "24" ? 0 : 1;
                             $max_hour = rgar($field, "timeFormat") == "24" ? 23 : 12;
                             if (!$is_valid_format || $hour < $min_hour || $hour > $max_hour || $minute < 0 || $minute >= 60) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = empty($field["errorMessage"]) ? __("Please enter a valid time.", "gravityforms") : $field["errorMessage"];
                             }
                             break;
                         case "website":
                             if (empty($value) || $value == "http://") {
                                 $value = "";
                                 if ($field["isRequired"]) {
                                     $field["failed_validation"] = true;
                                     $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required.", "gravityforms") : $field["errorMessage"];
                                 }
                             }
                             if (!empty($value) && !GFCommon::is_valid_url($value)) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = empty($field["errorMessage"]) ? __("Please enter a valid Website URL (i.e. http://www.gravityforms.com).", "gravityforms") : $field["errorMessage"];
                             }
                             break;
                         case "calculation":
                             $quantity_id = $field["id"] . ".3";
                             $quantity = rgget($quantity_id, $value);
                             if ($field["isRequired"] && rgblank($quantity) && !rgar($field, "disableQuantity")) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = rgempty("errorMessage", $field) ? __("This field is required.", "gravityforms") : rgar($field, "errorMessage");
                             } else {
                                 if (!empty($quantity) && (!is_numeric($quantity) || intval($quantity) != floatval($quantity) || intval($quantity) < 0)) {
                                     $field["failed_validation"] = true;
                                     $field["validation_message"] = __("Please enter a valid quantity", "gravityforms");
                                 }
                             }
                             break;
                         case "radio":
                             if (rgar($field, 'enableOtherChoice') && $value == 'gf_other_choice') {
                                 $value = rgpost("input_{$field['id']}_other");
                             }
                             if ($field["isRequired"] && rgar($field, 'enableOtherChoice') && $value == GFCommon::get_other_choice_value()) {
                                 $field["failed_validation"] = true;
                                 $field["validation_message"] = empty($field["errorMessage"]) ? __("This field is required.", "gravityforms") : $field["errorMessage"];
                             }
                             break;
                     }
                 }
             }
         }
         $custom_validation_result = apply_filters("gform_field_validation", array("is_valid" => rgar($field, "failed_validation") ? false : true, "message" => rgar($field, "validation_message")), $value, $form, $field);
         $custom_validation_result = apply_filters("gform_field_validation_{$form["id"]}", $custom_validation_result, $value, $form, $field);
         $custom_validation_result = apply_filters("gform_field_validation_{$form["id"]}_{$field["id"]}", $custom_validation_result, $value, $form, $field);
         $field["failed_validation"] = rgar($custom_validation_result, "is_valid") ? false : true;
         $field["validation_message"] = rgar($custom_validation_result, "message");
     }
     $is_valid = true;
     foreach ($form["fields"] as $f) {
         if (rgar($f, "failed_validation")) {
             $is_valid = false;
             break;
         }
     }
     $validation_result = apply_filters("gform_validation_{$form["id"]}", apply_filters("gform_validation", array("is_valid" => $is_valid, "form" => $form)));
     $is_valid = $validation_result["is_valid"];
     $form = $validation_result["form"];
     return $is_valid;
 }