验证不适用于关系字段 [英] Validation doesn't work on relation fields

查看:30
本文介绍了验证不适用于关系字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户和文件之间有 OnetoOne 关系,问题是每次我通过嵌入表单发布新文件时,验证都不起作用.
我尝试了 symfony doc 中提到的这个解决方案,以防止验证被嵌入作为正在验证的对象上的属性,我们需要在第一个字段中添加@Assert\Valid.
我也试图验证即使是字符串而不是文件,但这没有用.
顺便说一下,用户类中提到的任何验证都可以正常工作.
文件.php

I have OnetoOne relation between Users and Files, The problém is that every time i post a new file via embed form the validiation does not work .
I tried this solution mentioned in symfony doc To prevent validation on objects that are embedded as properties on an object being validated we need to add @Assert\Valid in the first filed.
Also i tried to validate even a string not a file and this didnt work.
By the way any validation mentioned in Users class, works fine.
Files.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;

/**
 * Files
 * @ORM\Table(name="files")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\FilesRepository")
 *
 * @Vich\Uploadable
 */
class Files implements \Serializable
{
    /**
     * @var int
     * 
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
 * 
 * @Assert\NotBlank()
 * @Assert\File(
 *     maxSize = "1024k",
 *     mimeTypes = {"application/pdf", "application/x-pdf"},
 *     mimeTypesMessage = "Please upload a valid PDF"
 * )
 * @Assert\File(maxSize="1M")
 * @Vich\UploadableField(mapping="upload_files", fileNameProperty="imageName", size="imageSize")
 * 
 * @var File
 */
protected $imageFile;

    /**
     * @ORM\Column(type="string", length=255)
     * 
     * @var string
     */
    protected $imageName;

    /**
     * @ORM\Column(type="datetime")
     *
     * @var string
     */
    protected $updatedAt;
    /**
     * @ORM\Column(type="datetime")
     *
     * @var string
     */
    protected $createdAt;
    public function getId()
    {
        return $this->id;
    }





    /**
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     *
     * 
     */
    public function setImageFile(File $image = null)
    {
        $this->imageFile = $image;
        $this->createdAt = new \DateTimeImmutable();

        if ($image) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->updatedAt = new \DateTimeImmutable();
        }

        return $this;
    }

    /**
     * @return File|null
     */
    public function getImageFile()
    {
        return $this->imageFile;
    }

    /**
     * @param string $imageName
     *
     * 
     */
    public function setImageName($imageName)
    {
        $this->imageName = $imageName;

        return $this;
    }

    /**
     * @return string|null
     */
    public function getImageName()
    {
        return $this->imageName;
    }

  /** @see \Serializable::serialize() */
    public function serialize()
    {
        return serialize(array(
            $this->id,
            $this->imageName,

        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized)
    {
        list (
            $this->id,

        ) = unserialize($serialized);
    }

    /**
     * @return string
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }

    /**
     * @param string $updatedAt
     */
    public function setUpdatedAt($updatedAt)
    {
        $this->updatedAt = $updatedAt;
    }


}

Users.php

<?php

namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUser;
use Symfony\Component\Validator\Constraints as Assert;


/**
 * Users 
 * @ORM\Table(name="users")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UsersRepository")
 *
 */
class Users extends  OAuthUser 
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * 
     * @ORM\OneToOne(targetEntity="AppBundle\Entity\Files",cascade={"persist"})
     * @ORM\JoinColumn(name="file_id", referencedColumnName="id")
     */
    public $cv;

UsersType.php

UsersType.php

class UsersType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->
->add('cv',FilesType::class)

文件类型.php

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder ->
    add('imageFile',


 VichFileType::class, [
            'required' => false,
            'allow_delete' => true, 
            'download_link' => true,
            'label' => false, 

        ]);



}

推荐答案

我找到了一个解决方案,但我仍然不知道为什么通过注释进行的验证不起作用.解决方案是通过 formType 约束通过验证:

I found a solution but i still dont know why the validation via annotation didint work. The solution is to passe validation via formType constrains:

/**
 * {@inheritdoc}
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->
        add('imageFile',
            VichFileType::class, [
                'constraints' => array(
                    new Image(array('minWidth' => 500, 'minHeight' => 350, 'mimeTypes' =>
                        array(
                            'image/jpeg',
                            'image/png',
                            'image/jpg',
                            'image/gif',
                        ),
                    )),
                    new File(array('maxSize' => 1024000, 'mimeTypes' =>
                        array(
                            'application/pdf',

                        ),)),),


                'required' => false,

                'allow_delete' => true,
                'download_link' => true,
                'label' => false,

            ]
    );
}

这篇关于验证不适用于关系字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆