|
11 | 11 | * Plugin URI: https://gravitywiz.com/gravity-forms-require-unique-values-for-different-fields/ |
12 | 12 | * Description: Require two or more fields on the same form to be different from each other. |
13 | 13 | * Author: Gravity Wiz |
14 | | - * Version: 0.2 |
| 14 | + * Version: 0.3 |
15 | 15 | * Author URI: https://gravitywiz.com/ |
16 | 16 | */ |
17 | 17 | class GW_Require_Unique_Values { |
@@ -83,8 +83,12 @@ public function validate( $result, $value, $form, $field ) { |
83 | 83 | } else { |
84 | 84 | $values = $this->get_group_values( $form, $field->id ); |
85 | 85 |
|
| 86 | + // Check if this should be validated as whole field |
| 87 | + $all_field_ids = array_merge( $this->_args['field_ids'], array( $this->_args['master_field_id'] ) ); |
| 88 | + $validate_as_whole = in_array( $field->id, $all_field_ids ); |
| 89 | + |
86 | 90 | // If the field has inputs, let's loop through them and check if they are unique. |
87 | | - if ( is_array( $field->inputs ) && ! empty( $field->inputs ) ) { |
| 91 | + if ( is_array( $field->inputs ) && ! empty( $field->inputs ) && ! $validate_as_whole ) { |
88 | 92 | $is_unique = true; |
89 | 93 |
|
90 | 94 | foreach ( $field->inputs as $input ) { |
@@ -194,6 +198,21 @@ public function get_filtered_value( $field, $input_id = null ) { |
194 | 198 | $value = $value[ $input_id ]; |
195 | 199 | } |
196 | 200 |
|
| 201 | + // When using a field ID (not input ID) for multi-input fields, combine all subfield values into one for validation. |
| 202 | + if ( ! $input_id && is_array( $field->inputs ) && is_array( $value ) ) { |
| 203 | + $all_field_ids = array_merge( $this->_args['field_ids'], array( $this->_args['master_field_id'] ) ); |
| 204 | + if ( in_array( $field->id, $all_field_ids ) ) { |
| 205 | + $combined_parts = array(); |
| 206 | + foreach ( $field->inputs as $input ) { |
| 207 | + $input_value = rgar( $value, $input['id'] ); |
| 208 | + if ( ! empty( $input_value ) ) { |
| 209 | + $combined_parts[] = $input_value; |
| 210 | + } |
| 211 | + } |
| 212 | + $value = array( implode( ' ', $combined_parts ) ); |
| 213 | + } |
| 214 | + } |
| 215 | + |
197 | 216 | $value = ! is_array( $value ) ? array( $value ) : $value; |
198 | 217 | $value = array_filter( $value ); |
199 | 218 |
|
|
0 commit comments