1

有没有一种简单的方法可以让 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;
    }
}
4

0 回答 0