Symfony – File Type assert not null on form edit

On a Symfony app in an entity I have a field logo that is a required field.

So I build a form and made the constraint validation inside.

My problem is that happen on the constraint I defined on the edition of the form when this one have already been filled on time. When I try to apply the constraint the object I get always tell me that my field logo is null.

Here is the code of my formType to make you undertand.

<?php

namespace AppBundleFormTypeStep;

use AppBundleEntityStepsStep14;
use DoctrineORMEntityManager;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormExtensionCoreTypeEmailType;
use SymfonyComponentFormExtensionCoreTypeFileType;
use SymfonyComponentFormExtensionCoreTypeTextType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;
use SymfonyComponentValidatorConstraintsCallback;
use SymfonyComponentValidatorContextExecutionContextInterface;

class Step14Type extends AbstractType
{

    private $em;

    public function __construct(EntityManager $em)
    {
        $this->em = $em;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        /** @var Step14 $step14 */
        $step14 = $options['step14'];

        $builder
            ->add('logo', FileType::Class, array(
                'label' => "* Logo",
                'required' => true,
                'data_class' => null
            ))
            ->add('name', TextType::Class, array(
                'label' => '* Name',
                'required' => true,
            ))
            ->add('surmane', TextType::Class, array(
                'label' => '* Surname',
                'required' => true,
            ))
            ->add('email', EmailType::class, array(
                'label' => '* Email',
                'required' => true,
            ))
        ;
      }

    public function validate(Step14 $step14, ExecutionContextInterface $context)
    {
        /** @var Step14 $searchStep14 */
        $searchStep14 = $this->em->getRepository(Step14::class)->findOneBy(['id' => $step14->getId()]);

        //If we are on creation mode (the user is filling the form for the firstTime)
        if ($searchStep14 === null) {
            if ($step14->getLogo() === null) {

                $context->buildViolation("You must upload your logo")
                    ->atPath('logo')
                    ->addViolation();

            } else {
                if ($step14->getLogo()->getMimeType() !== 'image/png' &&
                    $step14->getLogo()->getMimeType() !== 'image/jpeg' &&
                    $step14->getLogo()->getMimeType() !== 'image/gif'
                ) {
                    $context->buildViolation("Your logo file must be at the format .png, .jpeg, .jpg or .gif")
                        ->atPath('logo')
                        ->addViolation();
                }
            }
        } else {
            //If we are on edition mode
            //if no logo had already been uploaded
            if ($searchStep14->getLogo() === null && $step14->getLogo() === null) {

                $context->buildViolation("You must upload your logo")
                    ->atPath('logo')
                    ->addViolation();
            }
           
            if($step14->getLogo() !== null) {
                if ($step14->getLogo()->getMimeType() !== 'image/png' &&
                    $step14->getLogo()->getMimeType() !== 'image/jpeg' &&
                    $step14->getLogo()->getMimeType() !== 'image/gif'
                ) {
                    $context->buildViolation("Your logo file must be at the format .png, .jpeg, .jpg or .gif")
                        ->atPath('logo')
                        ->addViolation();
                }
            }
        }
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => Step14::class,
            'userEdition' => null,
            'step14' => null,
            'edition' => null,
            'constraints' => [
                new Callback([$this, 'validate']),
            ],
        ));
    }
}

My problem is that in my validate function I can’t get the original value of the object $step14, before the form submit because I would like to know if I am on edit mode or not, to not have to force the user to upload again his logo he already did it before and if he just want to edit his name, surname or email.

In my validate function I thought that by calling : $this->em->getRepository(Step14::class)->findOneBy(['id' => $step14->getId()]);
I would get the original value of my object but in fact I still get a null value while at this point nothing has been presisted end flush on that entity in my controller.

Indeed if I take an example where my logo has already been filled and I only try to update my name, in the validate function :

$step14->getLogo() return null and $searchStep14->getLogo() return null too

Does anyone have an idea on how I could fix this problem ?
Thanks by advance.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *