Forms Symfony向表单动态添加条目会导致TypeError

Forms Symfony向表单动态添加条目会导致TypeError,forms,symfony,Forms,Symfony,在我的表单中,嵌入了一组表单。其思想是使用jQuery(从集合中)添加或删除条目。jQuery部分工作正常。我知道我的实体也很好,因为我以前使用EasyAdminBundle来实现这个功能,这没有问题 问题是:当我通过jQuery添加条目并提交表单时,会出现以下错误: 类型错误:App\Entity\Answer::getIsCorrect()的返回值必须是boolean类型,返回null 这是父窗体: <?php namespace App\Form\Type; use App\En

在我的表单中,嵌入了一组表单。其思想是使用jQuery(从集合中)添加或删除条目。jQuery部分工作正常。我知道我的实体也很好,因为我以前使用EasyAdminBundle来实现这个功能,这没有问题

问题是:当我通过jQuery添加条目并提交表单时,会出现以下错误:

类型错误:App\Entity\Answer::getIsCorrect()的返回值必须是boolean类型,返回null

这是父窗体:

<?php

namespace App\Form\Type;

use App\Entity\Question;
use App\Form\DataTransformer\TagTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;

class QuestionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('answers', CollectionType::class, array(
                    'entry_type' => AnswerType::class,
                    'allow_add' => true,
                    'allow_delete' => true,
                    'delete_empty' => true,
                    'entry_options' => ['label' => false],
                    'prototype' => true,
                    'label' => false,
                    'by_reference' => false,
                )
            )
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(
            array(
                'data_class' => Question::class
            )
        );
    }
}
jQuery的结果:

<input type="checkbox" id="question_answers_3_isCorrect" name="question[answers][3][isCorrect]" value="1">
以下是实体
答案

这就是罪魁祸首

/**
 * @return mixed
 */
public function getIsCorrect() : bool
{
    return $this->isCorrect;
}
换成

/**
 * @return mixed
 */
public function getIsCorrect() : bool
{
    return boolval($this->isCorrect);
}

编辑:好的,我会尽量提供更多的信息。出现这样的错误是因为方法中的类型提示是错误的。例如,getIsCorrect方法说它返回bool,但实际上,当isCorrect属性未初始化时,它也返回null。就我个人而言,我建议您永久删除typehints,只保留phpdoc,但如果确实需要它们,请尝试在实体的构造函数中初始化字段并设置默认的db值

它是否有效?复选框是否正确?如果是的话,在你的Answer实体模型中,你应该将isCorrect的默认值设置为false,我认为。它不会,即使它被选中。好的,你使用的是PHP7吗?你能分享实体答案的模型吗?是的,使用了PHP7。我添加了实体。我想强调的是:使用DoctrineFixtures,我已经为问题实体添加了一些答案,这些答案显示在表单中。每个答案在生成的表单集合中都有一个条目。我可以更改此条目中的值,即将文本框留空、取消选中复选框或一起删除该条目。感谢您的回答,该异常确实不再出现。但是表单中的文本字段现在也有同样的问题。
Symfony\Component\Debug\Exception\FatalThrowableError:
Type error: Return value of App\Entity\Answer::getIsCorrect() must be of the type boolean, null returned

  at src/Entity/Answer.php:82
  at App\Entity\Answer->getIsCorrect()
     (vendor/symfony/property-access/PropertyAccessor.php:487)
  at Symfony\Component\PropertyAccess\PropertyAccessor->readProperty(array(object(Answer)), 'isCorrect')
     (vendor/symfony/property-access/PropertyAccessor.php:410)
  at Symfony\Component\PropertyAccess\PropertyAccessor->readPropertiesUntil(array(object(Answer)), object(PropertyPath), 1, true)
     (vendor/symfony/property-access/PropertyAccessor.php:179)
  at Symfony\Component\PropertyAccess\PropertyAccessor->getValue(object(Answer), object(PropertyPath))
     (vendor/symfony/form/Extension/Core/DataMapper/PropertyPathMapper.php:92)
  at Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapFormsToData(object(RecursiveIteratorIterator), object(Answer))
     (vendor/symfony/form/Form.php:630)

// Here the data is suddenly lost

  at Symfony\Component\Form\Form->submit(array(), true)
     (vendor/symfony/form/Form.php:574)
  at Symfony\Component\Form\Form->submit(array(array('text' => 'this gets lost')), true)
     (vendor/symfony/form/Form.php:574)

// Here the data comes in, now see above ↑

at Symfony\Component\Form\Form->submit(array('answers' => array(array('isCorrect' => '1', 'text' => 'foo'), array('text' => 'bar'), array('text' => 'baz'), array('text' => 'this gets lost'))), true)
     (vendor/symfony/form/Extension/HttpFoundation/HttpFoundationRequestHandler.php:113)
  at Symfony\Component\Form\Extension\HttpFoundation\HttpFoundationRequestHandler->handleRequest(object(Form), object(Request))
     (vendor/symfony/form/Form.php:500)
  at Symfony\Component\Form\Form->handleRequest(object(Request))
     (src/Controller/QuestionsController.php:79)
class Answer
{
    /**
     * @var
     * @ORM\Column(type="string")
     */
    protected $text;

    /**
     * @var
     * @ORM\Column(type="boolean")
     */
    protected $isCorrect;

    /**
     * @var
     * @ORM\ManyToOne(targetEntity="Question", inversedBy="answers", cascade={"persist"})
     */
    protected $question;

    /**
     * @return mixed
     */
    public function getText() : string
    {
        return $this->text;
    }

    /**
     * @param mixed $text
     */
    public function setText(string $text) : void
    {
        $this->text = $text;
    }

    /**
     * @return mixed
     */
    public function getIsCorrect() : bool
    {
        return $this->isCorrect;
    }

    /**
     * @param bool $isCorrect
     */
    public function setIsCorrect(bool $isCorrect) : void
    {
        $this->isCorrect = $isCorrect;
    }
}
/**
 * @return mixed
 */
public function getIsCorrect() : bool
{
    return $this->isCorrect;
}
/**
 * @return mixed
 */
public function getIsCorrect() : bool
{
    return boolval($this->isCorrect);
}