Pascal
Pascal

Reputation: 1

Symfony2 different image validation

at first - sorry for my bad english :-)

i am new on symfony and i got a problem with the image validation. i save all images in my project in one table. but i need different validation for profile images, news images etc.

/**
 * @ORM\ManyToOne(targetEntity="Image", cascade={"persist"})
 * @ORM\JoinColumn(name="teaser_right", referencedColumnName="id")
 */
private $teaserRight;

/**
 * @ORM\ManyToOne(targetEntity="Image", cascade={"persist"})
 * @ORM\JoinColumn(name="teaser_left", referencedColumnName="id")
 */
private $teaserLeft;

In the target entity i cannot do the following because different images need to different validate

/**
 * @ORM\Column(name="file", type="string", length=255, nullable=true)
 * @Assert\Image(
 *     minWidth = 350,
 *     minHeight = 350
 * )
 */

Any ideas?

Upvotes: 0

Views: 237

Answers (2)

Kamil Adryjanek
Kamil Adryjanek

Reputation: 3338

I think you need Validation Groups so you can add specific image validation to target entity for each group: profile, news etc.

/**
 * @ORM\Column(name="file", type="string", length=255, nullable=true)
 * @Assert\Image(
 *     minWidth = 350,
 *     minHeight = 350,
 *     groups = {"profile"}
 * )
 */

Upvotes: 1

Konrad Podgórski
Konrad Podgórski

Reputation: 2034

You can add validation constrains to form itself

This way you will have complete control of what each form allow. I guess you have separate forms for different images anyway

$formBuilder->add('uploadedFiles', ImageType::class, [
    'required' => false,
    'multiple' => true,
    'constraints' => [
        new \Symfony\Component\Validator\Constraints\Image([ /** options here **/])
    ]
]);

Upvotes: 0

Related Questions