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; }