[英]Symfony form with EntityType field yields a 'This value should not be blank' error if the field has a Valid() contraint in the object class
I have a Phone
class and a Country
class in a Symfony 2.8 project. 我在Symfony 2.8项目中有一个
Phone
类和一个Country
类。 One of the fields of the Phone
class is $country
which is a Country
object. Phone
类的一个字段是$country
,它是一个Country
对象。
I also have a form with an EntityType field, which I use to select from a list of the countries in my database. 我还有一个带有EntityType字段的表单,我用它来从我的数据库中的国家列表中进行选择。
My problem is that when I select a country from the dropdown menu generated by the EntityType field and submit the form, I get the error 'This value should not be blank.' 我的问题是,当我从EntityType字段生成的下拉菜单中选择一个国家并提交表单时,我收到错误“此值不应为空白”。 several times above the EntityField dropdown menu.
在EntityField下拉菜单上方几次。
What is weird is that I do a dump($phone)
after $form->handleRequest($request);
奇怪的是我在
$form->handleRequest($request);
之后进行dump($phone)
$form->handleRequest($request);
and the $country field shows the correct Country object fetched from the database. $ country字段显示从数据库中获取的正确Country对象。
This only happens when the @Assert\\Valid()
constraint is on the $country
field. 仅当
@Assert\\Valid()
约束在$country
字段上时才会发生这种情况。 Once i remove it the error goes away. 一旦我删除它,错误就会消失。 The thing is that I want to continue to validate the country field of my
Phone
class. 问题是我想继续验证我的
Phone
类的country字段。
Any ideas on why this is happening would be greatly appreciated! 任何关于为什么会发生这种情况的想法将不胜感激!
Phone
class: Phone
类:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Phone
*
* @ORM\Table(name="phones", uniqueConstraints={@ORM\UniqueConstraint(name="natural_pk", columns={"phone_name", "country_id", "phone_number", "extension"})})
* @ORM\Entity(repositoryClass="AppBundle\Repository\PhoneRepository")
* @ORM\HasLifecycleCallbacks
*/
class Phone
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*
* @Assert\Type(type="int")
*/
protected $id;
/**
* @var Country
*
* @ORM\ManyToOne(targetEntity="Country")
* @ORM\JoinColumn(nullable=false)
*
* @Assert\NotBlank()
* @Assert\Valid()
*/
protected $country;
/**
* @var string
*
* @ORM\Column(name="phone_number", type="string", length=20, nullable=false)
*
* @Assert\NotBlank()
* @Assert\Type(type="string")
* @Assert\Length(max=20)
*/
protected $phoneNumber;
public function __toString()
{
return $this->phoneNumber;
}
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set phoneNumber
*
* @param string $phoneNumber
*
* @return Phone
*/
public function setPhoneNumber($phoneNumber)
{
$this->phoneNumber = $phoneNumber;
return $this;
}
/**
* Get phoneNumber
*
* @return string
*/
public function getPhoneNumber()
{
return $this->phoneNumber;
}
/**
* Set country
*
* @param \AppBundle\Entity\Country $country
*
* @return Phone
*/
public function setCountry(\AppBundle\Entity\Country $country)
{
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return \AppBundle\Entity\Country
*/
public function getCountry()
{
return $this->country;
}
}
Country class: 国家级:
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;
}
}
I finally found the solution to the problem. 我终于找到了问题的解决方案。 It turns out that my
Country
class actually has another field from a bidirectional association called $provinces
and this field has a Valid()
constraint as well: 事实证明,我的
Country
类实际上有一个来自名为$provinces
的双向关联的另一个字段,并且该字段也有一个Valid()
约束:
/**
* @var Province[]
*
* @ORM\OneToMany(targetEntity="Province", mappedBy="country")
*
* @Assert\Valid()
*/
protected $provinces;
This was causing the error, I guess because if $provinces
is an empty array, the Country
object doesn't fulfill Symfony's Valid()
constraint. 这是导致错误的,我想因为如果
$provinces
是一个空数组, Country
对象不符合Symfony的Valid()
约束。
In order to solve it I had to either remove the Valid()
constraint on the $provinces
field in the Country class, or on the $country
field in the Phone
class. 为了解决这个问题,我必须删除Country类中
$provinces
字段的Valid()
约束,或者删除Phone
类中$country
字段的Valid()
约束。
You can disable validation by specifying non-existed validation group in you form type: 您可以通过在表单类型中指定不存在的验证组来禁用验证:
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['validation_groups' => ['no-validation']]); // Do not validate entities.
}
Didn't find a better solution :/ 没有找到更好的解决方案:/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.