Reputation: 4541
Using the choice type of Symfony framwork, we can decide de display lists, radio buttons or checkboxes playing with those two keys:
'multiple' => false,
'expanded' => true, //example for radio buttons
Let's say that instead of strings the value of the different choices given as an array in the 'choices' key are booleans :
$builder->add('myproperty', 'choice', array(
'choices' => array(
'Yes' => true,
'No' => false
),
'label' => 'My Property',
'required' => true,
'empty_value' => false,
'choices_as_values' => true
));
Using a list (select) to display the differnet choices there is no problem and when the form is displayed the right choice in the list is selected.
If I add the two keys(multiple and expanded) I talked about before to replace the list by radio buttons, there is no selected button for my field (though it worked with the select).
Somebody knows why ?
How to easily make it work ?
Thank you
Note : in fact I thought it would not works with any of then as the values are booleans and finally become strings but as it works for the list, I wonder why it does not work for the others.
Upvotes: 8
Views: 29484
Reputation: 306
My solution:
/**
* @var BooleanToStringTransformer $transformer
*/
private $transformer;
/**
* @param BooleanToStringTransformer $transformer
*/
public function __construct(BooleanToStringTransformer $transformer) {
$this->transformer = $transformer;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('myProperty', TextType::class, [
'empty_data' => false,
])
;
$builder->get('myProperty')->addModelTransformer($this->transformer);
}
<?php
namespace App\DataTransformer;
use Symfony\Component\Form\DataTransformerInterface;
/**
* Class BooleanToStringTransformer
*
* @package App\DataTransformer
*/
class BooleanToStringTransformer implements DataTransformerInterface
{
/**
* @param bool $boolean
*
* @return string
*/
public function transform($boolean): string
{
// transform the boolean to a string
return $boolean ? 'true' : 'false';
}
/**
* @param string $string
*
* @return bool
*/
public function reverseTransform($string): bool
{
// transform the string back to a boolean
return filter_var($string, FILTER_VALIDATE_BOOL);
}
}
Upvotes: 3
Reputation: 98
Another solution is to use Doctrine Lifecycle Callbacks and php Type Casting.
With this FormType:
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
//...
$builder->add('active', ChoiceType::class, [
'choices' => [
'active' => true,
'inactive' => false
]
])
And Entity like this :
//...
use Doctrine\ORM\Mapping as ORM;
/**
* ...
* @ORM\HasLifecycleCallbacks() /!\ Don't forget this!
* ...
*/
class MyEntity {
//..
/**
* @var bool
*
* @ORM\Column(name="active", type="boolean")
*/
private $active;
//...
/**
* @ORM\PrePersist()
*/
public function prePersist()
{
$this->active = (bool) $this->active; //Force using boolean value of $this->active
}
/**
* @ORM\PreUpdate()
*/
public function preUpdate()
{
$this->active = (bool) $this->active;
}
//...
}
Upvotes: 8
Reputation: 4541
I add a data transformer;
$builder->add('myproperty', 'choice', array(
'choices' => array(
'Yes' => '1',
'No' => '0'
),
'label' => 'My Property',
'required' => true,
'empty_value' => false,
'choices_as_values' => true
));
$builder->get('myProperty')
->addModelTransformer(new CallbackTransformer(
function ($property) {
return (string) $property;
},
function ($property) {
return (bool) $property;
}
));
It is magical : Now I have the right radio button checked and the right values in the entity.
Upvotes: 12