get_input_value_submission( 'input_' . $this->id, $this->inputName, $field_values, $get_from_post_global_var ); if ( is_array( $value ) ) { $value = array_map( 'trim', $value ); foreach ( $value as &$v ) { $v = trim( $v ); $v = $this->clean_value( $v ); } } else { if ( is_string( $value ) ) { $value = trim( $value ); $value = $this->clean_value( $value ); } } return $value; } /** * Ensures the POST value is in the correct number format. * * @since 2.4 * * @param $value * * @return bool|float|string */ public function clean_value( $value ) { if ( $this->numberFormat == 'currency' ) { $currency = new RGCurrency( GFCommon::get_currency() ); $value = $currency->to_number( $value ); } elseif ( $this->numberFormat == 'decimal_comma' ) { $value = GFCommon::clean_number( $value, 'decimal_comma' ); } elseif ( $this->numberFormat == 'decimal_dot' ) { $value = GFCommon::clean_number( $value, 'decimal_dot' ); } return $value; } public function validate( $value, $form ) { // 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 will be tested against the is_numeric() function to make sure it is in the right format. // If the POST value is an array then the field is inside a repeater so use $value. $raw_value = isset( $_POST[ 'input_' . $this->id ] ) && ! is_array( $_POST[ 'input_' . $this->id ] ) ? GFCommon::maybe_add_leading_zero( rgpost( 'input_' . $this->id ) ) : $value; $has_raw_value = ! rgblank( trim( $raw_value ) ); $requires_valid_number = $has_raw_value && ! $this->has_calculation(); $is_valid_number = $this->validate_range( $value ) && GFCommon::is_numeric( $raw_value, $this->numberFormat ); if ( $requires_valid_number && ! $is_valid_number ) { $this->failed_validation = true; $this->validation_message = empty( $this->errorMessage ) ? $this->get_range_message() : $this->errorMessage; } elseif ( $this->type == 'quantity' && $has_raw_value ) { if ( intval( $value ) != $value ) { $this->failed_validation = true; $this->validation_message = empty( $this->errorMessage ) ? esc_html__( 'Please enter a valid quantity. Quantity cannot contain decimals.', 'gravityforms' ) : $this->errorMessage; } elseif ( ( ! is_numeric( $value ) || intval( $value ) != floatval( $value ) || intval( $value ) < 0 ) ) { $this->failed_validation = true; $this->validation_message = empty( $this->errorMessage ) ? esc_html__( 'Please enter a valid quantity', 'gravityforms' ) : $this->errorMessage; } } } /** * Is the given value considered empty for this field. * * Adds a check to the parent method because a value of 0 returns a false positive. * * @since 2.7.1 * * @param $value * * @return bool */ public function is_value_empty( $value ) { $empty = parent::is_value_empty( $value ); if ( $empty && ! rgblank( $value ) ) { return false; } return $empty; } /** * Validates the range of the number according to the field settings. * * @param string $value A decimal_dot formatted string * * @return true|false True on valid or false on invalid */ private function validate_range( $value ) { if ( ! GFCommon::is_numeric( $value, 'decimal_dot' ) ) { return false; } $numeric_min = $this->numberFormat == 'decimal_comma' ? GFCommon::clean_number( $this->rangeMin, 'decimal_comma' ) : $this->rangeMin; $numeric_max = $this->numberFormat == 'decimal_comma' ? GFCommon::clean_number( $this->rangeMax, 'decimal_comma' ) : $this->rangeMax; if ( ( is_numeric( $numeric_min ) && $value < $numeric_min ) || ( is_numeric( $numeric_max ) && $value > $numeric_max ) ) { return false; } else { return true; } } public function get_range_message() { $min = $this->rangeMin; $max = $this->rangeMax; $numeric_min = $min; $numeric_max = $max; if ( $this->numberFormat == 'decimal_comma' ) { $numeric_min = empty( $min ) ? '' : GFCommon::clean_number( $min, 'decimal_comma', '' ); $numeric_max = empty( $max ) ? '' : GFCommon::clean_number( $max, 'decimal_comma', '' ); } $message = ''; if ( is_numeric( $numeric_min ) && is_numeric( $numeric_max ) ) { $message = sprintf( esc_html__( 'Please enter a number from %1$s to %2$s.', 'gravityforms' ), "$min", "$max" ); } elseif ( is_numeric( $numeric_min ) ) { $message = sprintf( esc_html__( 'Please enter a number greater than or equal to %s.', 'gravityforms' ), "$min" ); } elseif ( is_numeric( $numeric_max ) ) { $message = sprintf( esc_html__( 'Please enter a number less than or equal to %s.', 'gravityforms' ), "$max" ); } elseif ( $this->failed_validation ) { $message = esc_html__( 'Please enter a valid number.', 'gravityforms' ); } return $message; } public function get_field_input( $form, $value = '', $entry = null ) { $is_entry_detail = $this->is_entry_detail(); $is_form_editor = $this->is_form_editor(); $form_id = $form['id']; $id = intval( $this->id ); $field_id = $is_entry_detail || $is_form_editor || $form_id == 0 ? "input_$id" : 'input_' . $form_id . "_$id"; $size = $this->size; $disabled_text = $is_form_editor ? "disabled='disabled'" : ''; $class_suffix = $is_entry_detail ? '_admin' : ''; $class = $size . $class_suffix; $class = esc_attr( $class ); $instruction = ''; $read_only = ''; if ( ! $is_entry_detail && ! $is_form_editor ) { if ( $this->has_calculation() ) { // calculation-enabled fields should be read only $read_only = 'readonly="readonly"'; } else { $message = $this->get_range_message(); $validation_class = $this->failed_validation ? 'validation_message' : ''; if ( ! $this->failed_validation && ! empty( $message ) && empty( $this->errorMessage ) ) { $instruction = "