Php 如果带有EntityType字段的Symfony表单在对象类中具有有效的()约束,则会生成“此值不应为空”错误

Php 如果带有EntityType字段的Symfony表单在对象类中具有有效的()约束,则会生成“此值不应为空”错误,php,forms,validation,symfony,Php,Forms,Validation,Symfony,我在Symfony 2.8项目中有一个电话班和一个国家班。 Phone类的一个字段是$country,它是一个country对象 我还有一个带有EntityType字段的表单,我用它从数据库中的国家列表中进行选择 我的问题是,当我从EntityType字段生成的下拉菜单中选择一个国家并提交表单时,在EntityField下拉菜单上方多次出现错误“此值不应为空” 奇怪的是,我在$form->handleRequest$请求之后转储了$phone;$country字段显示从数据库中获取的正确国家对象

我在Symfony 2.8项目中有一个电话班和一个国家班。 Phone类的一个字段是$country,它是一个country对象

我还有一个带有EntityType字段的表单,我用它从数据库中的国家列表中进行选择

我的问题是,当我从EntityType字段生成的下拉菜单中选择一个国家并提交表单时,在EntityField下拉菜单上方多次出现错误“此值不应为空”

奇怪的是,我在$form->handleRequest$请求之后转储了$phone;$country字段显示从数据库中获取的正确国家对象

只有当@Assert\Valid约束位于$country字段上时,才会发生这种情况。一旦我删除它,错误就会消失。 问题是我想继续验证我的Phone类的country字段

任何关于为什么会发生这种情况的想法都将不胜感激

电话类别: 国家级:
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Country
 *
 * @ORM\Table(name="countries")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\CountryRepository")
 */
class Country
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     *
     * @Assert\Type(type="int")
     */
    protected $id;

    /**
     * @var string
     *
     * @ORM\Column(name="country_name", type="string", length=255, nullable=false)
     *
     * @Assert\NotBlank()
     * @Assert\Type(type="string")
     * @Assert\Length(max=255)
     */
    protected $countryName;

    /**
     * @var string
     *
     * @ORM\Column(name="phone_code", type="string", length=10, nullable=false)
     *
     * @Assert\NotBlank()
     * @Assert\Type(type="string")
     * @Assert\Length(max=10)
     */
    protected $phoneCode;

    public function __toString()
    {
        return $this->phoneCode;
    }

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set countryName
     *
     * @param string $countryName
     *
     * @return Country
     */
    public function setCountryName($countryName)
    {
        $this->countryName = $countryName;

        return $this;
    }

    /**
     * Get countryName
     *
     * @return string
     */
    public function getCountryName()
    {
        return $this->countryName;
    }

    /**
     * Set phoneCode
     *
     * @param string $phoneCode
     *
     * @return Country
     */
    public function setPhoneCode($phoneCode)
    {
        $this->phoneCode = $phoneCode;

        return $this;
    }

    /**
     * Get phoneCode
     *
     * @return string
     */
    public function getPhoneCode()
    {
        return $this->phoneCode;
    }
}

我终于找到了解决问题的办法。 事实证明,我的Country类实际上有一个双向关联中的另一个字段,名为$PROVICES,该字段也有一个有效的约束:

/**
 * @var Province[]
 *
 * @ORM\OneToMany(targetEntity="Province", mappedBy="country")
 *
 * @Assert\Valid()
 */
protected $provinces;
这是导致错误的原因,我想是因为如果$PROVICES是空数组,则Country对象不满足Symfony的有效约束


为了解决这个问题,我必须删除Country类中$provides字段的有效约束,或者Phone类中$Country字段的有效约束。

我终于找到了问题的解决方案。 事实证明,我的Country类实际上有一个双向关联中的另一个字段,名为$PROVICES,该字段也有一个有效的约束:

/**
 * @var Province[]
 *
 * @ORM\OneToMany(targetEntity="Province", mappedBy="country")
 *
 * @Assert\Valid()
 */
protected $provinces;
这是导致错误的原因,我想是因为如果$PROVICES是空数组,则Country对象不满足Symfony的有效约束


为了解决这个问题,我必须删除Country类中$provides字段或Phone类中$Country字段的有效约束。

您可以通过在表单类型中指定不存在的验证组来禁用验证:

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(['validation_groups' => ['no-validation']]); // Do not validate entities.
}

未找到更好的解决方案:/

您可以通过在表单类型中指定不存在的验证组来禁用验证:

/**
 * {@inheritdoc}
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(['validation_groups' => ['no-validation']]); // Do not validate entities.
}

没有找到更好的解决方案://

这些国家是否真的有效?这些国家是否真的有效?