Symfony2表单-多对多作为文本导致错误 [英] Symfony2 form - Many to Many as text causing errors

查看:40
本文介绍了Symfony2表单-多对多作为文本导致错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图寻找一种可能的解决方案,但是没有运气.

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.

我的解决方案是将其作为表单中的文本字段,然后在PrePersist上捕获它以搜索匹配的记录,然后将其应用于实体,然后再保存到数据库中.

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.

谢谢

哈里

 $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')
        ;

推荐答案

您需要数据转换器,以便在处理表单之前将字符串输入转换为实体.

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.

这篇关于Symfony2表单-多对多作为文本导致错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆