Skip to content

Commit

Permalink
Forms: Hide empty radio fields (#41379)
Browse files Browse the repository at this point in the history
  • Loading branch information
edanzer authored Jan 30, 2025
1 parent 9c55617 commit ecb5560
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Forms: Hide empty radio fields.
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,10 @@ class="below-label__label ' . ( $this->is_error() ? ' form-error' : '' ) . '"
* @return string HTML
*/
public function render_field( $type, $id, $label, $value, $class, $placeholder, $required, $required_field_text ) {
if ( ! $this->is_field_renderable( $type ) ) {
return null;
}

$class .= ' grunion-field';

$form_style = $this->get_form_style();
Expand Down Expand Up @@ -1141,6 +1145,33 @@ private function maybe_override_type() {
return $type;
}

/**
* Determines if a form field is valid.
*
* Add checks here to confirm if any given form field
* is configured correctly and thus should be rendered
* on the frontend.
*
* @param string $type - the field type.
*
* @return bool
*/
public function is_field_renderable( $type ) {
// Check for valid radio field.
if ( $type === 'radio' ) {
$options = (array) $this->get_attribute( 'options' );
$non_empty_options = array_filter(
$options,
function ( $option ) {
return $option !== '';
}
);
return count( $non_empty_options ) > 0;
}

return true;
}

/**
* Gets the form style based on its CSS class.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: bugfix

Forms: Hide empty radio fields.

0 comments on commit ecb5560

Please sign in to comment.