@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,不带括号。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...