I'm trying to render a checkbox inside a form but got the next error:
TransformationFailedException in Form.php line 1149: Unable to transform value for property path "[active]": Expected a Boolean.
Yes, what I send is an integer 0 or 1, but twig get them as string and it has sense but I really can't find how to render the checkbox.
This is the controller:
$form = $form->add('active', 'checkbox', array('label' => 'User active?','required' => false));
And this is the Twig side:
{{ form_widget(form.active, { attr: { 'class': 'form-control' }}) }}
Any idea?
In your form add a model transformer as @Rinat suggested:
$form->add('active', 'checkbox', array('label' => 'User active?','required' => false));
$form->get('active')
->addModelTransformer(new CallbackTransformer(
function ($activeAsString) {
// transform the string to boolean
return (bool)(int)$activeAsString;
},
function ($activeAsBoolean) {
// transform the boolean to string
return (string)(int)$activeAsBoolean;
}
));
More details here: http://symfony.com/doc/current/cookbook/form/data_transformers.html