首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@Assert\NotBlank验证在symfony 4中不以嵌入式形式工作

@Assert\NotBlank验证在symfony 4中不以嵌入式形式工作
EN

Stack Overflow用户
提问于 2020-08-13 08:49:04
回答 1查看 666关注 0票数 0

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

BlockType Form

代码语言:javascript
复制
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,
        ]);
    }
}

块实体

代码语言:javascript
复制
/**
 * @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

代码语言:javascript
复制
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实体

代码语言:javascript
复制
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;
    }

}
EN

回答 1

Stack Overflow用户

发布于 2020-08-13 09:01:26

通过查看文档,您应该在发送表单之前调用$validator->validate($object)

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63391452

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档