选择字段类型上的Symfony2数据转换器

时间:2013-08-22 17:15:35

标签: jquery forms symfony choice fieldtype

我将一步一步地使用How to use Data Transformers

问题是如果我想用Choice类型做什么呢?我用jQuery动态填充哪些内容?

我测试了他们提供的示例(没有创建自定义类型..)并且它与文本字段类型一起工作100%,但是一旦我将其更改为选择并给它空选择它不起作用,这与我在加载页面后用jQuery填充选项有关吗?

实施例

模型 [选择使用查询构建器和实体字段类型加载的模型实体...]

数字 [首先清空选择,当模型更改时我为该模型的数字发出AJAX请求]

如果我将Number作为文本字段并且我手动键入有效数字(查看数据库)它可以工作,但是如果我将它留给jQuery和Choice类型它会返回一个表单错误,其值为无效模型。

在这两种情况下,我在处理表单之前都在执行print_r($ request-> request),在两种情况下都提交Number => 1在这个例子中是正确的,但不知何故,当它的类型是选择时,它不会转换数据,但是当它的文本就是它时。

这是jQuery填充数字选择框的数据的方式:

<option value=”1”>1234ABCDEFG</option>
是的,我正在使用Id进行转换,这将是所选选项的值。

1 个答案:

答案 0 :(得分:2)

确定。你需要做的是听preSubmit表单事件,然后基本上接受提交的值,方法是将它添加到你的选择元素。

http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

=============================================== ======

我没有看你的粘贴箱,但这里有一个似乎对我有用的例子。这是一个简单的性别选择列表,我添加了另一个选项客户端。 preSubmit侦听器只是使用包含所提交内容的选项替换默认的性别选择选项。你应该能够添加数据变换的东西,并且好好去。

namespace Cerad\Bundle\TournBundle\Form\Type\Test;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormFactoryInterface;

use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class DynamicFormType extends AbstractType
{
    public function getName() { return 'cerad_tourn_test_dynamic'; }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('gender', 'choice', array(
            'choices'   => array('m' => 'Male', 'f' => 'Female'),
            'required'  => false,
        ));
        $builder->addEventSubscriber(new DynamicFormListener($builder->getFormFactory()));
    }
}
class DynamicFormListener implements EventSubscriberInterface
{
    private $factory;

    public function __construct(FormFactoryInterface $factory)
    {
        $this->factory = $factory;
    }

    public static function getSubscribedEvents()
    {
        return array(
            FormEvents::PRE_SUBMIT   => 'preSubmit',
            FormEvents::PRE_SET_DATA => 'preSetData',
        );
    }
    public function preSetData(FormEvent $event)
    {
        // Don't need
        return;
    }
    public function preSubmit(FormEvent $event)
    {
        $data = $event->getData();
        $gender = $data['gender'];
        if (!$gender) return; // If nothing was actually chosen

        $form = $event->getForm();

        /* =================================================
         * All we need to do is to replace the choice with one containing the $gender value
         * Once this is done $form->isValid() will pass
         *
         * I did attempt to just add the option to the existing gender choice 
         * but could not see how to do it.  
         * $genderForm = form->get('gender'); // Returns a Form object
         * $genderForm->addNewOptionToChoicesList ???
         * 
         * Might want to look up 'whatever' but that only comes into play
         * if the form fails validation and you paas it back to the user
         * You could also use client side javascript to replace 'whatever' with the correct value
         */
        $form->add($this->factory->createNamed('gender','choice', null, array(
            'choices'   => array($gender => 'whatever'),
            'required'  => false,
            'auto_initialize' => false,
        )));
        return;
    }
}