Symfony2 form - Many to Many as text causing errors

余生颓废 提交于 2019-12-24 03:50:35

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!