Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required at false for FileType #24

Open
SelMaK-fr opened this issue Dec 13, 2024 · 1 comment
Open

Required at false for FileType #24

SelMaK-fr opened this issue Dec 13, 2024 · 1 comment

Comments

@SelMaK-fr
Copy link

Hello,

I have a FileType field in my form but I don't want to make it mandatory, unfortunately, "required" to false doesn't work.

Why?

The processing of this form is done in a Service and via a single save() method (create and update)

How to make this field not mandatory while keeping the constraints and verification in my Form class?

Thanks :)

Form class:

class MySuperForm implements FormFactoryInterface
{
    public function buildForm(mixed $data = null, ?array $args = [])
    {
           $builder = (new FormBuilder('my_form', $data))
                      ->add('uploadedLogo', FileType::class, [
                'label' => 'Choisissez un fichier',
                'constraints' => [
                    new FileConstraint\Extension([
                        'extensions' => ['jpg', 'gif', 'png'],
                    ]),
                    new FileConstraint\MimeType([
                        'mime_types' => ['image/jpeg', 'image/gif', 'image/png'],
                    ]),
                ],
                'required' => false,
                'mapped' => false,
            ]);
          ///
   }
}

Controller:

    public function actionForm($request, $response, ?int $id): ResponseInterface
    {
        $datas = null;
        if($id != null){
            $datas = $this->em->getRepository(MyEntity::class)->find($id);
        }
        $form = $this->formFactory->create(MySuperForm::class, $datas);
        $form->handleRequest();

        // Envoi des données en POST
        if($form->isSubmitted() && $form->isValid()){
            $data = [
                'form' => $form->getRequestData()['form_data'],
                'uploadedFile' => $request->getUploadedFiles()['form_file'],
            ];
            return $this->service->save($data, $datas);
        }
     }

Service:

   public function save(array $data, ?MyEntity $myEntity): ResponseInterface
   {
      if (!empty($data['uploadedFile']['uploadedLogo'])) {
         /// traitment
       }
   }
@andyexeter
Copy link
Member

Hey @SelMaK-fr,

Sorry for the delay - I was busy with work and then had time off over the Christmas period.

I'll try and take a look at this in the coming days :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants