symfony custom form field not valid

422 views Asked by At

I am using symfony 6 with everything updated to date.

I created a form type with which I intend to receive a url or an image and internally transform the image into a url that will indicate where it is stored on the server, or keep the given url. The idea is to be able to receive one or the other (url or file) and only return as data the new url.

Here is my code:

parent form

class EditProfileForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('imgProfile', ImageType::class, [
                'required' => false,
                'label' => "Profile Image",
                'bucket' => 'tw-user-files',
                'key' => $options["key"],
                'constraints' => [
                    new File([
                        'maxSize' => '7M',
                        'mimeTypes' => [
                            'image/*'
                        ],
                        'mimeTypesMessage' => 'That is not a valid image format',
                    ])
                ],
                'setter' => function (User &$user, string $data, FormInterface $form): void {
                    $user->setImgProfile($data);
                }
            ])
        ;
    }
}

ImageType

class ImageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $bucketName = $options["bucket"];
        $bucket = $this->flySystemS3->getBucket($bucketName);
        $constraints = $options["constraints"];

        $builder->add('url', UrlType::class, [
                'error_bubbling' => false,
                'required' => false,
            ])
            ->add('file', FileType::class, [
                "error_bubbling" => false,
                "multiple" => false,
                'required' => false,
                'attr' => [
                    'accept' => 'image/*'
                ],
                'constraints' => $constraints
            ])
            ->addViewTransformer(new UrlOrFileToImage($bucket, $options["key"]));
    }
}

Data tranformer

class UrlOrFileToImage implements DataTransformerInterface
{
    public function transform($value)
    {
        return [
            "url" => "",
            "file" => null
        ];
    }

    public function reverseTransform($value)
    {
        if (!empty($value["url"])) {
            return $value;
        }

        if (!empty($value["file"])) {
            return $this->fileToImage($value["file"]);
        }

        return "#";
    }

    private function fileToImage(UploadedFile $file): string
    {
        $fileName = $this->uploadToServer();

        return $fileName;
    }
}

the situation is that when validating the parent form I get the error "The file could not be found.", this only happens when validating the parent form, the child form "ImageType" passes all validations and does not generate error.

I debugged it step by step and when I debug it, it passes correctly, the validation is correct and returns everything ok. So I think it is some validation that takes too long, and at the time of debugging gives it a chance to validate it, and when I let it run the code, the validation fails.

0

There are 0 answers