首页 > 解决方案 > @Assert\NotBlank 验证在 symfony 4 中不能以嵌入式形式工作

问题描述

我有一个名为BlockType的表单,它有一个名为BlockHeroStaticImageType的嵌入式表单。嵌入表单BlockHeroStaticImageType的名为“ title ”的字段包含验证注释,如下所示(请参阅 下面的BlockHeroStaticImage 实体)。当我在表单中将标题留空并尝试保存表单时,不会触发表单验证。验证应该失败,但事实并非如此。我签入了控制器,尽管标题为空,但它仍返回 true。我在这里想念什么?请帮忙。@Assert\NotBlank()$form->isValid()

块类型表格

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;
    }

}

标签: phpsymfony4

解决方案


通过查看文档,您应该$validator->validate($object)在发送表格之前致电。

此外,不确定它现在是否有效,但文档中提供的用于添加 NotBlank 约束的语法是@Assert\NotBlank,不带括号。


推荐阅读