Skip to content

Commit f507348

Browse files
committed
gw-require-unique-values.php: Added support for whole-field validation for multi-input fields
1 parent 8eef25a commit f507348

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

gravity-forms/gw-require-unique-values.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Plugin URI: https://gravitywiz.com/gravity-forms-require-unique-values-for-different-fields/
1212
* Description: Require two or more fields on the same form to be different from each other.
1313
* Author: Gravity Wiz
14-
* Version: 0.2
14+
* Version: 0.3
1515
* Author URI: https://gravitywiz.com/
1616
*/
1717
class GW_Require_Unique_Values {
@@ -83,8 +83,12 @@ public function validate( $result, $value, $form, $field ) {
8383
} else {
8484
$values = $this->get_group_values( $form, $field->id );
8585

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+
8690
// 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 ) {
8892
$is_unique = true;
8993

9094
foreach ( $field->inputs as $input ) {
@@ -194,6 +198,21 @@ public function get_filtered_value( $field, $input_id = null ) {
194198
$value = $value[ $input_id ];
195199
}
196200

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+
197216
$value = ! is_array( $value ) ? array( $value ) : $value;
198217
$value = array_filter( $value );
199218

0 commit comments

Comments
 (0)