问题
I have tried looking around for a possible solution to this but with no luck.
What I have is a Many to many relationship between properties and postcodes, I can't display the postcodes in a select for example due to the amount of possible entries.
My solution was to have it as a text field in the form and then catch it on PrePersist to search for the matching record and then apply this to the entity before persisting to the db.
The problem is when the form is validating it is still trying to pass the string value to the setter which is expecting an entity object.
Is there anyway to prevent this from causing an error?
I have attached my form code for you.
Thanks,
Harry
$propertyData = new PropertyData();
$builder
->add('reference')
->add('listing_type', 'choice', array('choices' => $propertyData->getListingTypes()))
->add('listing_status', 'choice', array('choices' => $propertyData->getStatusList()))
->add('title')
->add('house_number')
->add('address_line_1')
->add('address_line_2')
->add('town', 'text', array('data_class'=> 'Minos\Bundle\PropertyBundle\Entity\UtilTown'))
->add('county')
->add('country')
->add('council')
->add('region')
->add('postcode', 'text', array('data_class'=> 'Minos\Bundle\PropertyBundle\Entity\UtilPostcode'))
->add('short_description')
->add('long_description')
->add('size_sq_ft')
->add('floor_level')
->add('property_age')
->add('tenure_type', 'choice', array('choices' => $propertyData->getTenureTypes()))
->add('garage')
->add('num_living_rooms')
->add('num_bathrooms')
->add('num_bedrooms')
->add('num_floors')
->add('num_receptions')
->add('property_type')
//->add('prices')
;
回答1:
You need a data transformer to convert your string input to an entity before processing the form.
$builder
// ...
->add('postcode', 'text', array(
'data_class'=> 'Minos\Bundle\PropertyBundle\Entity\UtilPostcode'
))
// ...
;
$builder->get('postcode')->addModelTransformer(new CallbackTransformer(
//Render an entity to a string to display in the text input
function($originalInput){
$string = $originalInput->getPostcode();
return $string;
},
//Take the form submitted value and convert it before processing.
//$submittedValue will be the string because you defined
// it in the builder that way
function($submittedValue){
//Do whatever to fetch the postcodes entity:
$postcodeEntity = $entityManager->find('AppBundle\postcodes', $submittedValue);
return $postcodeEntity;
}
));
This is just an example (I haven't tested it), you will need to change some stuff to match how your entities look.
来源:https://stackoverflow.com/questions/32227295/symfony2-form-many-to-many-as-text-causing-errors