'这不是一个有效的选项'自定义formtype提交错误

时间:2013-02-19 07:38:19

标签: php ajax symfony symfony-forms

有一种简单的方法可以让symfony中的选择字段正确验证通过js添加的数据吗?因此,例如,您加载一个空字段,然后使用js / ajax调用填充它,然后选择一个按下提交的选项,但验证程序始终抛出此选项是无效错误...

为了给出一些背景知识,我有一个自定义表单类型,它使用选择类型作为父类,还有一个自定义数据转换器,将选项转换为实体(我可以确认它是有效的,因为如果我将表单类型更改为文本并手动输入与我想要选择的选项对应的id,表单提交正确。)

有什么想法吗?我很乐意提供您可能想要查看的任何文件吗?

修改nullstateType.php

<?php

namespace ISFP\Index\IndexBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use ISFP\Index\IndexBundle\Form\Transformer\nullstateTransformer;
use Doctrine\Common\Persistence\ObjectManager;

class nullstateType extends AbstractType
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @param ObjectManager $om
     */
    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $transformer = new nullstateTransformer($this->om);
        $builder->prependNormTransformer($transformer);
    }

    public function setAllowedValues(OptionsResolverInterface $resolver) 
    {
        return array( 'widget' => array('choice'));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'invalid_message' => 'The selected state does not exist',
            'property_path' => false
        );
    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'nullstate';
    }
}

nullstateTransformer.php

<?php

namespace ISFP\Index\IndexBundle\Form\Transformer;

use Symfony\Component\Form\DataTransformerInterface;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Doctrine\Common\Persistence\ObjectManager;
use ISFP\Index\IndexBundle\Entity\State;

class nullstateTransformer implements DataTransformerInterface
{
    /**
     * @var ObjectManager
     */
    private $om;

    /**
     * @param ObjectManager $om
     */
    public function __construct(ObjectManager $om)
    {
        $this->om = $om;
    }

    /**
     * Transforms an object (state) to a string (id).
     *
     * @param  Issue|null $state
     * @return string
     */
    public function transform($state)
    {
        if (null === $state) {
            return "";
        }

        return $this->om->getRepository('ISFPIndexEntityBundle:State')->getId();
    }

    /**
     * Transforms a string (id) to an object (state).
     *
     * @param  string $id
     * @return Issue|null
     * @throws TransformationFailedException if object (state) is not found.
     */
    public function reverseTransform($id)
    {
        if (!$id) {
            return null;
        }

        $state = $this->om
            ->getRepository('ISFPIndexEntityBundle:State')
            ->findOneById(intval($id))
        ;

        if (null === $state) {
            throw new TransformationFailedException(sprintf(
                'An state with id "%s" does not exist!',
                $id
            ));
        }

        return $state;
    }
}

0 个答案:

没有答案