Skip to content

Commit

Permalink
\gpadvs-enable-add-new-option.php`: Fixed an issue where adding a new…
Browse files Browse the repository at this point in the history
… option may result in failed validation during form submission.
  • Loading branch information
saifsultanc committed Jan 13, 2024
1 parent a3f9c9c commit d960308
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions gp-advanced-select/gpadvs-enable-add-new-option.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public function __construct( $args = array() ) {

public function init() {

if ( ! is_callable( 'gp_advanced_select' ) ) {
return;
}

add_filter( 'gform_pre_render', array( $this, 'load_form_script' ), 10, 2 );

// Advanced Select has to beat GF's default Chosen init script to the punch so uses `gform_pre_render` to register
Expand All @@ -37,7 +41,7 @@ public function init() {

add_filter( 'gform_pre_render', array( $this, 'allow_created_choices_in_save_and_continue' ), 10, 3 );

add_filter( 'gform_validation', array( $this, 'validate' ) );
add_filter( 'gform_pre_render', array( $this, 'skip_validate_state_for_adv_select_field' ), 10, 1 );
}

public function load_form_script( $form, $is_ajax_enabled ) {
Expand Down Expand Up @@ -156,22 +160,13 @@ public function allow_created_choices_in_save_and_continue( $form, $ajax, $field
return $form;
}

public function validate( $validation_result ) {
$form = $validation_result['form'];
public function skip_validate_state_for_adv_select_field( $form ) {
foreach ( $form['fields'] as &$field ) {
// Do not "fail" validation for the Advanced Select field.
if ( $field['id'] === $this->_args['field_id'] ) {
$field->failed_validation = false;
$validation_result['is_valid'] = true;
} else {
// Other fields still need to be tested for failed validations.
if ( $field->failed_validation ) {
$validation_result['is_valid'] = false;
break;
}
if ( $field->id == $this->_args['field_id'] ) {
$field->validateState = false;
}
}
return $validation_result;
return $form;
}

}
Expand Down

0 comments on commit d960308

Please sign in to comment.