Installation:
composer require bukashk0zzz/booleantype-bundle
For Symfony Flex projects, ensure extra.symfony.allow-contrib is enabled.
For non-Flex projects, manually add the bundle to config/bundles.php:
return [
// ...
Bukashk0zzz\BooleanTypeBundle\BooleanTypeBundle::class => ['all' => true],
];
First Use Case:
Replace a standard checkbox field in a form with the custom boolean type:
use Bukashk0zzz\BooleanTypeBundle\Form\Type\BooleanType;
$builder->add('isActive', BooleanType::class, [
'label' => 'Enable Feature',
'required' => false,
]);
Where to Look First:
src/Form/Type/BooleanType.php (custom type logic).Resources/views/BooleanType/boolean_widget.html.twig (default Twig template).Basic Boolean Field:
// In a FormType class
$builder->add('newsletterSubscribed', BooleanType::class, [
'label' => 'Subscribe to Newsletter',
'attr' => ['class' => 'custom-checkbox'],
]);
Dynamic Boolean Fields:
Use in entity forms (e.g., UserType):
$builder->add('accountVerified', BooleanType::class, [
'label' => 'Verified',
'disabled' => true, // Disable if immutable
]);
Integration with Validation:
Combine with Symfony’s Assert\Type or Assert\True/Assert\False:
use Symfony\Component\Validator\Constraints as Assert;
$builder->add('termsAccepted', BooleanType::class, [
'constraints' => [
new Assert\True(message: 'You must accept the terms.'),
],
]);
Custom Templates: Override the default Twig template by creating:
templates/BooleanType/boolean_widget.html.twig
Example:
{# Custom switch-like toggle #}
<input type="checkbox" {{ widget.attributes }} data-toggle="switch">
API/JSON Forms:
For API platforms (e.g., Symfony UX Turbo), ensure the field returns 0/1:
$builder->add('isPublished', BooleanType::class, [
'mapped' => true,
'normalization_context' => ['groups' => ['api']],
]);
bool or boolean:
/**
* @ORM\Column(type="boolean")
*/
private bool $isActive;
# config/packages/translation.yaml
en:
boolean:
label: 'Enable {0}Feature{1}'
$builder->add('isActive', BooleanType::class, [
'label' => 'Enable %feature%',
'translation_domain' => 'boolean',
]);
Boolean vs. Integer:
true/false to 1/0 by default. If your database expects boolean, ensure your Doctrine mapping uses type="boolean" (not smallint).Normalizer to enforce types:
use Symfony\Component\Form\DataTransformerInterface;
class BooleanToIntTransformer implements DataTransformerInterface {
public function transform($value) {
return $value ? 1 : 0;
}
public function reverseTransform($value) {
return filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
}
Register it in your BooleanType extension.Template Overrides:
{{ form_widget() }} variable is passed correctly. The default template expects:
<input type="checkbox"
{{ widget.attributes.add({
'checked': widget.vars.value ? 'checked' : ''
}) }}
>
CSRF and Hidden Fields:
false values (unlike Symfony’s default checkbox). If you need this behavior, extend the type:
class CustomBooleanType extends AbstractType {
public function getParent() {
return BooleanType::class;
}
public function getBlockPrefix() {
return 'custom_boolean';
}
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder->addModelTransformer(new BooleanToIntTransformer());
}
}
Symfony 6+ Deprecations:
autoconfigure: true by default. If you encounter issues, manually configure the bundle in config/packages/boolean_type.yaml:
bukashk0zzz_boolean_type:
default_options: { required: false }
Field Not Rendering:
php bin/console debug:container bukashk0zzz_boolean_type.boolean).BooleanType::class).Value Not Persisting:
dump($form->getData()) to confirm 1/0 values.bool:
public function setIsActive(bool $isActive): self {
$this->isActive = $isActive;
return $this;
}
Template Caching:
boolean_widget.html.twig aren’t reflected:
php bin/console cache:clear
Custom Widgets: Extend the bundle to support radio buttons or switches:
// src/Form/Extension/CustomBooleanExtension.php
use Symfony\Component\Form\AbstractTypeExtension;
use Bukashk0zzz\BooleanTypeBundle\Form\Type\BooleanType;
class CustomBooleanExtension extends AbstractTypeExtension {
public function getExtendedType(): string {
return BooleanType::class;
}
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults([
'widget_type' => 'switch', // 'checkbox', 'radio', or 'switch'
]);
}
}
Register the extension in services.yaml:
services:
App\Form\Extension\CustomBooleanExtension:
tags: [form.type_extension]
Dynamic Options: Use form events to modify options dynamically:
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
$form->add('isAdmin', BooleanType::class, [
'disabled' => !$data->isSuperAdmin(),
]);
});
Localization:
Override translations for true/false labels:
# config/packages/translation.yaml
en:
boolean:
true_label: 'Yes'
false_label: 'No'
Then in your template:
{{ form_label(form.isActive) }}
<input type="checkbox"
{{ widget.attributes.add({
'checked': widget.vars.value ? 'checked' : '',
'data-true': form.vars.data ? 'Yes' : '',
'data-false': form.vars.data ? 'No' : ''
}) }}
>
How can I help you explore Laravel packages today?