I have a boolean field that I've put in a form as a choice field (yes or no). I would get 0 or 1 without data transformer. I added a view BooleanToStringTransformer (which seemed reasonable) :
$builder
->add(
$builder->create('myBooleanField', 'choice', array(
'choices' => array(true => 'Yes', false => 'No'),
))
->addViewTransformer(new BooleanToStringTransformer('1'))
)
And when I try to display the form, I get the error "Expected a Boolean.". My field is set to false before creating the form though.
I tried to set it as a model transformer: the form is dispayed, but when I submit it my field is declared invalid.
What am I doing wrong?
Edit: I nearly got it now.
- I used a model transformer instead of a view transformer (the choice field works with strings or integers, not booleans)
- I changed my choice list from
array(true => 'Yes', false => 'No')
toarray('yes' => 'Yes', 'no' => 'No')
So the code now looks like ->addModelTransformer(new BooleanToStringTransformer('yes'))
Data transformation works, except that my field is always set to true, whatever value I choose.
What's wrong?
Another workaround can be:
The answer is: I shouldn't have thought the default Symfony BooleanToStringDataTransformer was doing the job. It returns null for a false value instead of a string.
So I created my own datatransformer:
You seem to have used a View transformer instead of a Model transformer. You'll need to reverse-transform 0/1 to boolean in the Model transformer if the underlying model expects boolean values.
.. or you might have missed to implement the reverse-transform method in your view transformer.
Read more about the difference between View and Model transformers here.