Skip to content

Commit

Permalink
Remove empty issuers field during form render.
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Jan 10, 2025
1 parent 9225108 commit ef7f29f
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/IssuersField.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ public function __construct( $properties = [] ) {
add_action( 'gform_editor_js_set_default_values', [ __CLASS__, 'editor_js_set_default_values' ] );
}

// Filters.
if ( ! has_filter( 'gform_pre_render', [ $this, 'maybe_remove_empty_issuers_field' ] ) ) {
\add_filter( 'gform_pre_render', [ $this, 'maybe_remove_empty_issuers_field' ] );
}

if ( ! has_filter( 'gform_field_validation', [ $this, 'maybe_validate_empty_issuers_field' ] ) ) {
\add_filter( 'gform_field_validation', [ $this, 'maybe_validate_empty_issuers_field' ], 10, 4 );
}

if (
! isset( $this->formId )
&&
Expand Down Expand Up @@ -673,4 +682,47 @@ public static function editor_js_set_default_values() {
break;
<?php
}

/**
* Remove issuers field without any choices.
*
* @param array $form Form.
* @return array
*/
public function maybe_remove_empty_issuers_field( $form ) {
foreach ( $form['fields'] as $key => $field ) {
if ( self::TYPE !== $field['type'] ) {
continue;
}

if ( 0 !== count( $field['choices'] ) ) {
continue;
}

unset( $form['fields'][ $key ] );
}

return $form;
}

/**
* Skip validation for issuers fields without any choices.
*
* @param array $result Validation result.
* @param mixed $value Field value.
* @param array $form Form.
* @param array $field Field.
* @return array
*/
public function maybe_validate_empty_issuers_field( $result, $value, $form, $field ) {
if ( self::TYPE !== $field['type'] ) {
return $result;
}

if ( 0 === count( $field['choices'] ) ) {
$result['is_valid'] = true;
}

return $result;
}
}

0 comments on commit ef7f29f

Please sign in to comment.