Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions gravity-forms/gw-require-unique-values.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Plugin URI: https://gravitywiz.com/gravity-forms-require-unique-values-for-different-fields/
* Description: Require two or more fields on the same form to be different from each other.
* Author: Gravity Wiz
* Version: 0.2
* Version: 0.3
* Author URI: https://gravitywiz.com/
*/
class GW_Require_Unique_Values {
Expand Down Expand Up @@ -52,6 +52,10 @@ public function init() {
add_filter( sprintf( 'gform_field_validation_%s', $this->_args['form_id'] ), array( $this, 'validate' ), 10, 4 );
}

private function get_all_field_ids() {
return array_merge( $this->_args['field_ids'], array( $this->_args['master_field_id'] ) );
}

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

if ( ! $this->is_applicable_field( $field ) ) {
Expand Down Expand Up @@ -83,8 +87,12 @@ public function validate( $result, $value, $form, $field ) {
} else {
$values = $this->get_group_values( $form, $field->id );

// Check if this should be validated as whole field
$all_field_ids = $this->get_all_field_ids();
$validate_as_whole = in_array( $field->id, $all_field_ids );

// If the field has inputs, let's loop through them and check if they are unique.
if ( is_array( $field->inputs ) && ! empty( $field->inputs ) ) {
if ( is_array( $field->inputs ) && ! empty( $field->inputs ) && ! $validate_as_whole ) {
$is_unique = true;

foreach ( $field->inputs as $input ) {
Expand Down Expand Up @@ -194,6 +202,21 @@ public function get_filtered_value( $field, $input_id = null ) {
$value = $value[ $input_id ];
}

// When using a field ID (not input ID) for multi-input fields, combine all subfield values into one for validation.
if ( ! $input_id && is_array( $field->inputs ) && is_array( $value ) ) {
$all_field_ids = $this->get_all_field_ids();
if ( in_array( $field->id, $all_field_ids ) ) {
$combined_parts = array();
foreach ( $field->inputs as $input ) {
$input_value = rgar( $value, $input['id'] );
if ( ! empty( $input_value ) ) {
$combined_parts[] = $input_value;
}
}
$value = array( implode( ' ', $combined_parts ) );
}
}

$value = ! is_array( $value ) ? array( $value ) : $value;
$value = array_filter( $value );

Expand Down
Loading