Reputation: 4561
Here is some code of my Type Class. For one of the property I use a ModelTransform to display what I want :
$builder->add('myProperty', 'text', array(
'label' => 'MyLabel',
'required' => true,
'disabled' => true
));
$builder->get('myProperty')
->addModelTransformer(new CallbackTransformer(
function($propertyFrom) {
return (round($propertyFrom/ 100, 3)) + 1;
},
function($propertyTo) {
return round((($propertyTo- 1) * 100), 3);
}
));
it works good when I display my form in the twig.
I would like to get the entity from my form. I guessed, assuming I have my Form object, I would do like this :
$entity = $form->getViewData()
I wanted to have the entity with the value of the fields passed through the ModelTransform. Though I have the value of field with no transformation.
Finally I wonder what is the difference between $form->getData() and $form->getViewData().
How can I get the entity with the values transformed by my ModelTransformer ?
Upvotes: 1
Views: 289
Reputation: 9585
This is a kind of limitation because compound forms having an object or array as underlying data, don't keep their view data synchronized with their children's view data. https://github.com/symfony/symfony/issues/18683#issuecomment-249676768
You can get the difference thus:
// if myProperty value is equal to 23
$form->get('myProperty')->getData(); //output: 23
$form->get('myProperty')->getNormData(); //output: 1.23
$form->get('myProperty')->getViewData(); //output: "1.23"
Upvotes: 1
Reputation: 4561
It is a Symfony bug that does not apply transformer via the getViewData on the object. But it does it on the field.
getViewData vs. getNormData and DataTransformers - different results depending on context
Upvotes: 1