[英]@Assert\NotBlank validation not working in embedded form in symfony 4
I have a form called BlockType which has an embedded form called BlockHeroStaticImageType .我有一个名为BlockType的表单,它有一个名为BlockHeroStaticImageType的嵌入式表单。 The field called ' title ' of the embedded form BlockHeroStaticImageType contains validation annotation
@Assert\NotBlank()
as shown below (see BlockHeroStaticImage Entity below).嵌入表单BlockHeroStaticImageType的名为“ title ”的字段包含验证注释
@Assert\NotBlank()
,如下所示(参见下面的BlockHeroStaticImage 实体)。 When I leave the title as empty in the form and try to save the form, the form validation is not triggered.当我在表单中将标题留空并尝试保存表单时,不会触发表单验证。 The validation should fail, but it is not the case.
验证应该失败,但事实并非如此。 I checked
$form->isValid()
in the controller and it is returning true despite the title being emtpy.我在 controller 中检查
$form->isValid()
,尽管标题为空,但它返回 true。 What am I missing here?我在这里想念什么? Please help.
请帮忙。
BlockType Form块类型表格
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,
]);
}
}
Block Entity阻止实体
/**
* @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 Form 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 Entity 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;
}
}
By looking in documentation you are supposed to call $validator->validate($object)
before sending form.通过查看文档,您应该在发送表单之前调用
$validator->validate($object)
。
Also, not sure if it works now but syntax provided in documentation for adding NotBlank constraint is @Assert\NotBlank
, without brackets.此外,不确定它现在是否有效,但文档中提供的用于添加 NotBlank 约束的语法是
@Assert\NotBlank
,不带括号。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.