[英]@Assert\NotBlank validation not working in embedded form in symfony 4
我有一个名为BlockType的表单,它有一个名为BlockHeroStaticImageType的嵌入式表单。 嵌入表单BlockHeroStaticImageType的名为“ title ”的字段包含验证注释@Assert\NotBlank()
,如下所示(参见下面的BlockHeroStaticImage 实体)。 当我在表单中将标题留空并尝试保存表单时,不会触发表单验证。 验证应该失败,但事实并非如此。 我在 controller 中检查$form->isValid()
,尽管标题为空,但它返回 true。 我在这里想念什么? 请帮忙。
块类型表格
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;
}
}
通过查看文档,您应该在发送表单之前调用$validator->validate($object)
。
此外,不确定它现在是否有效,但文档中提供的用于添加 NotBlank 约束的语法是@Assert\NotBlank
,不带括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.