如何解决@Assert \ NotBlank验证无法在Symfony 4中以嵌入式形式运行
我有一个名为 BlockType 的表单,该表单有一个名为 BlockHeroStaticImageType 的嵌入式表单。嵌入式表单 BlockHeroStaticImageType 的名为“ 标题”的字段包含验证注释@Assert\NotBlank()
,如下所示(请参见下面的 BlockHeroStaticImage实体)。
当我在表单中将 title 保留为空并尝试保存表单时,不会触发表单验证。验证应该失败,但事实并非如此。我检查了控制器中的$form->isValid()
,尽管标题为空,它仍返回true。我在这里想念什么?请帮忙。
BlockType表单
class BlockType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder,array $options)
{
$builder
->add('content',TextareaType::class,[
'required' => false,'attr' => [
'class' => 'ckeditor','data-field' => 'content'
]
])
->add('blockHeroStaticImages',CollectionType::class,[
'entry_type' => BlockHeroStaticImageType::class,'entry_options' => ['label' => false],'label' => 'Hero Static Image','allow_add' => true,'allow_delete' => true,'by_reference' => false,'help' => '<a data-collection="add" class="btn btn-info btn-sm" href="#">Add Hero Static Image</a>','help_html' => true,'attr' => [
'data-field' => 'blockHeroStaticImages'
]
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Block::class,]);
}
}
块实体
/**
* @ORM\Entity(repositoryClass="App\Repository\BlockRepository")
*/
class Block
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="text",nullable=true)
*/
private $content;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Block\BlockHeroStaticImage",mappedBy="block",orphanRemoval=true,cascade={"persist"})
*/
private $blockHeroStaticImages;
public function __construct()
{
$this->blockHeroStaticImages = new ArrayCollection();
}
...
/**
* @return Collection|BlockHeroStaticImage[]
*/
public function getBlockHeroStaticImages(): Collection
{
return $this->blockHeroStaticImages;
}
public function addBlockHeroStaticImage(BlockHeroStaticImage $blockHeroStaticImage): self
{
if (!$this->blockHeroStaticImages->contains($blockHeroStaticImage)) {
$this->blockHeroStaticImages[] = $blockHeroStaticImage;
$blockHeroStaticImage->setBlock($this);
}
return $this;
}
public function removeBlockHeroStaticImage(BlockHeroStaticImage $blockHeroStaticImage): self
{
if ($this->blockHeroStaticImages->contains($blockHeroStaticImage)) {
$this->blockHeroStaticImages->removeElement($blockHeroStaticImage);
// set the owning side to null (unless already changed)
if ($blockHeroStaticImage->getBlock() === $this) {
$blockHeroStaticImage->setBlock(null);
}
}
return $this;
}
}
BlockHeroStaticImageType表单
class BlockHeroStaticImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder,array $options)
{
$builder
->add('title',TextType::class,[
'required' => false
]);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => BlockHeroStaticImage::class,]);
}
}
BlockHeroStaticImage实体
use Symfony\Component\Validator\Constraints as Assert;
/**
* @ORM\Entity(repositoryClass="App\Repository\Block\BlockHeroStaticImageRepository")
*/
class BlockHeroStaticImage
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string",length=255)
* @Assert\NotBlank()
*/
private $title;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Block",inversedBy="blockHeroStaticImages")
* @ORM\JoinColumn(nullable=false)
*/
private $block;
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
}
解决方法
通过查看documentation,您应该在发送表格之前致电$validator->validate($object)
。
此外,不确定现在是否可以使用,但是文档中用于添加NotBlank约束的语法为@Assert\NotBlank
,不带括号。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。