Set a Minimum Submission Value

Require a numeric Slider field’s submitted value to meet a minimum without changing the visual minimum of the slider track.

For example, a slider can visually run from 0% to 7% while rejecting values below 0.10%. Because the slider defaults to 0%, this also prevents an untouched slider from being submitted.

Instructions

  1. Install this snippet by following the steps here: https://gravitywiz.com/documentation/how-do-i-install-a-snippet/

  2. Update the configuration at the bottom of the snippet.

Code

Filename: gps-minimum-submission-value.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
 * Gravity Perks // Sliders // Set a Minimum Submission Value
 * https://gravitywiz.com/documentation/gravity-forms-sliders/
 *
 * Require a numeric Slider field's submitted value to meet a minimum without
 * changing the visual minimum of the slider track.
 *
 * For example, a slider can visually run from 0% to 7% while rejecting values
 * below 0.10%. Because the slider defaults to 0%, this also prevents an untouched
 * slider from being submitted.
 *
 * Instructions:
 *
 * 1. Install this snippet by following the steps here:
 *    https://gravitywiz.com/documentation/how-do-i-install-a-snippet/
 *
 * 2. Update the configuration at the bottom of the snippet.
 */
class GPS_Minimum_Submission_Value {

	private $_args;

	public function __construct( $args = array() ) {

		$this->_args = wp_parse_args( $args, array(
			'form_id'            => false,
			'slider_field_id'    => false,
			'minimum_value'      => false,
			'validation_message' => '',
		) );

		add_filter(
			sprintf(
				'gform_field_validation_%d_%d',
				$this->_args['form_id'],
				$this->_args['slider_field_id']
			),
			array( $this, 'validate' ),
			10,
			4
		);

	}

	public function validate( $result, $value, $form, $field ) {

		if ( ! $result['is_valid'] || ! class_exists( 'GP_Sliders' ) || ! GP_Sliders::is_slider_field( $field ) ) {
			return $result;
		}

		if ( rgar( $field, 'sliderMode' ) === 'choices' || ! is_numeric( $this->_args['minimum_value'] ) ) {
			return $result;
		}

		$submitted_value = is_array( $value )
			? rgar( $value, $field->id . '.1' )
			: $value;

		if ( rgblank( $submitted_value ) ) {
			return $result;
		}

		$number_format = rgar( $field, 'numberFormat' );
		$number_format = in_array( $number_format, array( 'currency', 'decimal_comma', 'decimal_dot' ), true )
			? $number_format
			: 'decimal_dot';

		$numeric_value = GFCommon::clean_number( $submitted_value, $number_format );

		if ( ! is_numeric( $numeric_value ) ) {
			return $result;
		}

		if ( (float) $numeric_value < (float) $this->_args['minimum_value'] ) {
			$result['is_valid'] = false;
			$result['message']  = $this->_args['validation_message']
				? $this->_args['validation_message']
				: sprintf(
					'Please select a value greater than or equal to %s.',
					$this->_args['minimum_value']
				);
		}

		return $result;
	}

}

# Configuration

new GPS_Minimum_Submission_Value( array(
	'form_id'            => 123,
	'slider_field_id'    => 4,
	'minimum_value'      => 0.10,
	'validation_message' => 'Please select an interest rate of at least 0.10%.',
) );

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.