Skip to content

EasyAdmin Boolean Field

Edit this page

This field displays the true/false value of a boolean property.

In form pages (edit and new) it looks like this (it's like an advanced <input type="checkbox"> created with JavaScript):

Default style of EasyAdmin boolean field

In read-only pages (index and detail) it renders either as a static Yes/No label or as a dynamic switch/toggle that can flip the value when clicking on it.

Basic Information

  • PHP Class: EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField
  • Doctrine DBAL Type used to store this value: boolean
  • Symfony Form Type used to render the field: CheckboxType
  • Rendered as:

    1
    <input type="checkbox">

Options

hideValueWhenFalse

Use this option to not display anything when the field value is false. This is useful to simplify the backend display in listings where most rows have the same false value and you want to ignore those and better highlight the rows with the true value:

1
yield BooleanField::new('...')->hideValueWhenFalse();

Keep in mind that:

  • This option is ignored when using the renderAsSwitch() option, which always displays a switch/toggle with the field value;
  • This option is only applied to the index page; in the detail page you will always see the field value to avoid any confusion.

hideValueWhenTrue

Use this option to not display anything when the field value is true. This is useful to simplify the backend display in listings where most rows have the same true value and you want to ignore those and better highlight the rows with the false value:

1
yield BooleanField::new('...')->hideValueWhenTrue();

Keep in mind that:

  • This option is ignored when using the renderAsSwitch() option, which always displays a switch/toggle with the field value;
  • This option is only applied to the index page; in the detail page you will always see the field value to avoid any confusion.

renderAsSwitch

By default, in read-only pages (index and detail) this field is rendered as a dynamic switch/toggle that can flip the underlying value when clicking on it. This switch is rendered with the Switch component. If you prefer to not allow changing the property value in this way, use this option:

1
yield BooleanField::new('...')->renderAsSwitch(false);

renderAsSuccessSwitch

By default the switch uses a neutral color when it's turned on. Use this option to color the "on" state with the same color as the success (green) buttons:

1
yield BooleanField::new('...')->renderAsSuccessSwitch();

This method also enables the switch rendering, so you don't need to call renderAsSwitch() too. The color is applied everywhere the field is rendered as a switch (the index toggle and the edit/new forms). The "off" state always uses the neutral color. The same applies to the two options below.

renderAsWarningSwitch

Same as renderAsSuccessSwitch(), but the "on" state uses the same color as the warning (amber) buttons:

1
yield BooleanField::new('...')->renderAsWarningSwitch();

renderAsDangerSwitch

Same as renderAsSuccessSwitch(), but the "on" state uses the same color as the danger (red) buttons:

1
yield BooleanField::new('...')->renderAsDangerSwitch();
This work, including the code samples, is licensed under a Creative Commons BY-SA 3.0 license.
TOC
    Version