Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove empty issuers field during form render. #48

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
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;
}
}
Loading