Dynamically Set Entry Min/Max From Field Value

Instructions

See “Where do I put snippets?” in our documentation for installation instructions.

Code

Filename: gpnf-limit-entry-min-max-from-field.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
<?php
/**
 * Gravity Perks // Nested Forms // Dynamically Set Entry Min/Max From Field Value
 * https://gravitywiz.com/documentation/gravity-forms-nested-forms/
 */
class GP_Nested_Forms_Dynamic_Entry_Min_Max {

	private $_args = array();

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

		// set our default arguments, parse against the provided arguments, and store for use throughout the class
		$this->_args = wp_parse_args( $args, array(
			'parent_form_id'       => false,
			'nested_form_field_id' => false,
			'default_max'          => 0, // Max to fallback to if the dependent field isn't populated
			'default_min'          => 0, // Min to fallback to if the dependent field isn't populated
			'max_field_id'         => null,
			'min_field_id'         => null, // (Optional)
		) );

		// do version check in the init to make sure if GF is going to be loaded, it is already loaded
		add_action( 'init', array( $this, 'init' ) );

	}

	public function init() {

		// make sure we're running the required minimum version of Gravity Forms
		if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
			return;
		}

		// time for hooks
		add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );
		add_filter( 'gform_register_init_scripts', array( $this, 'add_init_script' ), 5, 2 );
		add_action( 'gform_validation', array( $this, 'validate' ) );

	}

	public function load_form_script( $form, $is_ajax_enabled ) {

		if ( $this->is_applicable_form( $form ) && ! has_action( 'wp_footer', array( $this, 'output_script' ) ) ) {
			add_action( 'wp_footer', array( $this, 'output_script' ) );
			add_action( 'gform_preview_footer', array( $this, 'output_script' ) );
		}

		return $form;
	}

	public function validate( $validation_result ) {

		if ( ! $this->is_applicable_form( $validation_result['form']['id'] ) ) {
			return $validation_result;
		}

		$form                    = $validation_result['form'];
		$nested_form_field_id    = $this->_args['nested_form_field_id'];
		$nested_form_field_value = rgpost( "input_{$nested_form_field_id}" );
		$nested_form_field       = GFAPI::get_field( $form, $nested_form_field_id );

		$entry_ids   = explode( ',', $nested_form_field_value );
		$entry_count = empty( $nested_form_field_value ) ? 0 : count( $entry_ids );

		$min_field_id = str_replace( '.', '_', $this->_args['min_field_id'] );
		$max_field_id = str_replace( '.', '_', $this->_args['max_field_id'] );

		$raw_min = rgpost( "input_{$min_field_id}" );
		$raw_max = rgpost( "input_{$max_field_id}" );

		if ( empty( $raw_min ) ) {
			$raw_min = $this->_args['default_min'];
		}

		if ( empty( $raw_max ) ) {
			$raw_max = $this->_args['default_max'];
		}

		$min = apply_filters( 'gpnf_entry_limit_min', $raw_min, $entry_count, $entry_ids, null, $form );
		$max = apply_filters( 'gpnf_entry_limit_max', $raw_max, $entry_count, $entry_ids, null, $form );

		$has_validation_error = false;

		foreach ( $validation_result['form']['fields'] as &$field ) {

			if ( $field['failed_validation'] ) {
				$has_validation_error = true;
			}

			if ( $field['id'] !== $nested_form_field_id || ! $this->should_field_be_validated( $form, $field ) ) {
				continue;
			}

			if ( $min !== null && $min !== '' && $entry_count < $min ) {
				$field['failed_validation'] = true;
				// translators: first placeholder is number, second is a noun
				$field['validation_message'] = sprintf( __( 'Please enter a minimum of %1$d %2$s', 'gp-nested-forms' ), $min, $min > 1 ? $nested_form_field->get_items_label() : $nested_form_field->get_item_label() );

				$has_validation_error = true;
			}

			if ( $max !== null && $max !== '' && $entry_count > $max ) {
				$field['failed_validation'] = true;
				// translators: first placeholder is number, second is a noun
				$field['validation_message'] = sprintf( __( 'Please enter a maximum of %1$d %2$s', 'gp-nested-forms' ), $max, $max > 1 ? $nested_form_field->get_items_label() : $nested_form_field->get_item_label() );

				$has_validation_error = true;
			}
		}

		$validation_result['is_valid'] = ! $has_validation_error;

		return $validation_result;

	}

	public function output_script() {
		?>

		<script type="text/javascript">

			(function ($) {

				window.GPNFDynamicEntryMax = function (args) {

					var self = this;

					// copy all args to current object: (list expected props)
					for (prop in args) {
						if (args.hasOwnProperty(prop))
							self[prop] = args[prop];
					}

					self.init = function () {

						var attrReadyMaxFieldId = typeof self.maxFieldId === 'string' ? self.maxFieldId.replace('.', '_') : self.maxFieldId;
						var maxFieldId = 'input_' + self.parentFormId + '_' + attrReadyMaxFieldId;

						// GF uses "1" for the input index in the HTML id attribute for Single Product fields. Weird.
						var $maxField = $( '#input_' + self.parentFormId + '_' + parseInt( self.maxFieldId ) );
						if ( $maxField.parents( '.gfield' ).hasClass( 'gfield--input-type-singleproduct' ) ) {
							maxFieldId = 'input_' + self.parentFormId + '_' + parseInt( self.maxFieldId ) + '_1';
						}

						gform.addFilter('gpnf_entry_limit_max', function (max, currentFormId, currentFieldId) {
							if (self.parentFormId != currentFormId || self.nestedFormFieldId != currentFieldId) {
								return max;
							}

							var $maxField = $( '#' + maxFieldId );
							var value = parseInt($maxField.val());

							return isNaN(value) ? Infinity : (value || self.defaultMax);
						});

						gform.addAction( 'gform_input_change', function( el, formId, fieldId ) {
							// Force Knockout to recalculate the max when the number has changed
							if ( el.id === maxFieldId ) {
								const gpnfViewModel = window[ 'GPNestedForms_{0}_{1}'.gformFormat( self.parentFormId, self.nestedFormFieldId ) ]?.viewModel;

								// Use the standard Knockout method if available.
								if ( typeof gpnfViewModel?.entries?.valueHasMutated === 'function' ) {
									gpnfViewModel.entries.valueHasMutated();
								// Fallback for scenarios where 'entries' is a computed observable (no valueHasMutated).
								// Trigger reactivity by reassigning a shallow copy of the observable array.
								} else if ( typeof gpnfViewModel?.entriesRaw === 'function' ) {
									gpnfViewModel.entriesRaw( gpnfViewModel.entriesRaw()?.slice() );
								}
							}
						} );

					};

					self.init();

				}

			})(jQuery);

		</script>

		<?php
	}

	public function add_init_script( $form ) {

		if ( ! $this->is_applicable_form( $form ) ) {
			return;
		}

		$args = array(
			'parentFormId'      => $this->_args['parent_form_id'],
			'nestedFormFieldId' => $this->_args['nested_form_field_id'],
			'defaultMax'        => $this->_args['default_max'],
			'maxFieldId'        => $this->_args['max_field_id'],
		);

		$script = 'new GPNFDynamicEntryMax( ' . json_encode( $args ) . ' );';
		$slug   = implode( '_', array(
			'gpnf_dynamic_entry_max_template',
			$this->_args['parent_form_id'],
			$this->_args['nested_form_field_id'],
		) );

		GFFormDisplay::add_init_script( $this->_args['parent_form_id'], $slug, GFFormDisplay::ON_PAGE_RENDER, $script );

	}

	public function is_applicable_form( $form ) {

		$form_id = isset( $form['id'] ) ? $form['id'] : $form;

		return empty( $this->_args['parent_form_id'] ) || (int) $form_id === (int) $this->_args['parent_form_id'];
	}

	public function should_field_be_validated( $form, $field ) {

		if ( (int) $field['pageNumber'] !== (int) GFFormDisplay::get_source_page( $form['id'] ) ) {
			return false;
		}

		if ( GFFormsModel::is_field_hidden( $form, $field, array() ) ) {
			return false;
		}

		return true;
	}

}

# Configuration

new GP_Nested_Forms_Dynamic_Entry_Min_Max( array(
	'parent_form_id'       => 4,
	'nested_form_field_id' => 3,
	'max_field_id'         => 1,
	'min_field_id'         => 2, // (Optional)
) );

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.