无法转换属性路径的值:预期布尔值(用于复选框)

时间:2018-06-07 23:31:59

标签: php symfony

我正在使用Symfony表单,该表单由一些数据预先填充(如果我能找到用户,我会预先填充它的数据)。

我收到此错误:

Unable to transform value for property path "[terms]": Expected a Boolean.

我使用terms类型的字段checkbox。 Symfony版本是2.8.28。

我的控制器代码是:

class SignupController extends ResourceController {
    public function newSignupAction(Request $request)
    {
        $resource = new Signup();

        $form = $this->createForm($this->createNewFormType(), $resource, array(
            'action' => $url,
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        if ($participant = $this->getUser()->getParticipant()) {
            $participation = new Participation();
            $participation->setParticipant($participant);

            $signup->addParticipation($participation);

            $form->setData($signup);
        }
    }
}

注册类如下所示:

class Signup extends ExtraFieldEntity 
{
    public function addParticipation(Participation $participation)
    {
        $participation->setSignup($this);

        $this->participations[] = $participation;

        return $this;
    }
}


class ParticipantBasicType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstName')
            ->add('lastName')
            ->add('extraFields', 'extra_field_collection', array(
                'group' => 'participant',
            ));

        $builder->addEventListener(
            FormEvents::POST_SET_DATA,
            function (FormEvent $event) use ($builder) {
                $form = $event->getForm();

                if (!$data = $event->getData()) {
                    return;
                }

                /** @var PersistentCollection $extraFields */
                $extraFields = $data->getExtraFields();

                if ($extraFields->isEmpty()) {
                    return;
                }

                foreach ($extraFields->getValues() as $extraField) {
                    /** @var Value $extraField */
                    if ($form->get('extraFields')->has($extraField->getDefinition()->getIdentifier())) {
                        $form->get('extraFields')
                            ->get($extraField->getDefinition()->getIdentifier())
                            ->setData($extraField);
                    }
                }
            }
        );
    }
}

应在表单上呈现额外字段。构建额外字段如下所示:

class ExtraFieldCollectionType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        .....

        $builder->add(
            $builder->create(
                $identifier,
                $type,
                $parameters
            )->addModelTransformer(
                new ScalarToExtraFieldValueTransformer($identifier, $definition, $type)
            )
        );
    }

}

它调用自定义模型转换器:

  class ScalarToExtraFieldValueTransformer implements DataTransformerInterface
    {
        private $identifier;

        private $definition;

        private $type;

        public function __construct($identifier, $definition, $type)
        {
            $this->identifier = $identifier;
            $this->definition = $definition;
            $this->type = $type;
        }

        public function transform($value)
        {
            $result = '';

            if (is_null($value)) {
                return $result;
            }

            if (is_object($value)) {
                /* @var Value $value */
                $result = $value->getValue();
            } elseif (is_string($value)) {
                if ($this->resolveType($this->type) == 'integer') {
                    $result = explode(',', $value);
                }
            }

            settype($result, $this->resolveType($this->type));

            return $result;
        }

        public function reverseTransform($value)
        {
            return new Value(
                $value,
                $this->definition
            );
        }

        private function resolveType($type)
        {
            switch ($type) {
                case 'choice':
                    return 'string';
                case 'checkbox':
                    return 'boolean';
                    break;
                default:
                    return 'string';
                    break;
            }
        }
}

由于我可以追踪问题,因此只针对checkbox类型:

vendor/symfony/symfony/src/Symfony/Component/Form/Form.php

在这种方法中:

private function normToView($value)
{
    // Scalar values should  be converted to strings to
    // facilitate differentiation between empty ("") and zero (0).
    // Only do this for simple forms, as the resulting value in
    // compound forms is passed to the data mapper and thus should
    // not be converted to a string before.
    if (!$this->config->getViewTransformers() && !$this->config->getCompound()) {
        return null === $value || is_scalar($value) ? (string) $value : $value;
    }

    try {
        foreach ($this->config->getViewTransformers() as $transformer) {
            $value = $transformer->transform($value);
        }
    } catch (TransformationFailedException $exception) {
        throw new TransformationFailedException(
            'Unable to transform value for property path "'.$this->getPropertyPath().'": '.$exception->getMessage(),
            $exception->getCode(),
            $exception
        );
    }

    return $value;
}

正在调用vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/BooleanToStringTransformer.php

关于我做错什么的任何建议?谢谢!

添加(参与者对象的定义)

class Participant extends ExtraFieldEntity
{
    /**
     * @ORM\ManyToMany(targetEntity="\Entity\ExtraField\Value", cascade={"persist", "remove"})
     */
    protected $extraFields;


    public function __construct()
    {
        parent::__construct();

        $this->participations = new ArrayCollection();
    }

    public function addParticipation(Participation $participations)
    {
        $this->participations[] = $participations;

        return $this;
    }

}

0 个答案:

没有答案