pvlkns/symfony-dropzone

Symfony Form Type

1.0.0 2024-02-05 22:50 UTC

This package is not auto-updated.

Last update: 2024-10-02 00:25:56 UTC


README

Extends the SymfonyForm component. Adds the new form type DropzoneType Use DropzoneType in form with relation other entity "One Form contain 2 entities relation".

Example : form with "Item" entity and DropZoneType link with "Attachment" entity

This is custom version of : https://github.com/emr-dev/symfony-dropzone

Installing

composer require ethsam/symfony-dropzone

Add the dropzone library to your project in template

<script src="https://unpkg.com/dropzone@6.0.0-beta.1/dist/dropzone-min.js"></script>
<link href="https://unpkg.com/dropzone@6.0.0-beta.1/dist/dropzone.css" rel="stylesheet" type="text/css" />

Usage

public function buildForm(\Symfony\Component\Form\FormBuilderInterface $builder, array $options)
{ 

    // userFiles is OneToMany
    $builder->add('userFiles', DropzoneType::class, [
        'class' => File::class,
        'maxFiles' => 6,
        'uploadHandler'=>'uploadHandler',  // route name
        'removeHandler'=> 'removeHandler'// route name
   ]),
   ->add('arrayIdMedia', TextType::class, ['mapped' => false]); //hide this type after tests
}

Examples route uploadHandler/removeHandler

    /**
     * @Route("/uploadhandler", name="uploadHandler")
     */
    public function uploadhandler(Request $request, ImageUploader $uploader) {

        $dateNow = new \DateTime('now');
        $doc = $uploader->upload($request->files->get('file'));
        $file = new Attachment();
        $file->setCreatedAt($dateNow);
        $file->setUpdatedAt($dateNow);
        $file->setImageFile($doc);

        $this->entityManager->persist($file);
        $this->entityManager->flush();
        return new JsonResponse([ "id" => $file->getId() ]);
    }


    /**
     * @Route("/removeHandler/{id}", name="removeHandler")
     */
    public function removeHandler(Request $request, $id) {

        $file = $this->repoAttachment->findOneBy(['id' => $id]);
        $idFile = $file->getId();

        $this->entityManager->remove($file);
        $this->entityManager->flush();

        return new JsonResponse([ "id" => $idFile ]);
    }

Example get data convert to array and findby for persist

    public function addClassifield(Request $request, EntityManagerInterface $entityManager): Response
    {
        $item = new Item();
        $form = $this->createForm(AddPropertyType::class, $item);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $item = $form->getData();

            $arrayItemsMedia = explode(',',$form->get("arrayIdMedia")->getData());
            foreach ($arrayItemsMedia as $key => $value) {
                $mediaObject = $this->repoAttachment->findOneBy(['id' => intval($value)]);
                $item->addAttachment($mediaObject);
            }

            $entityManager->persist($item);
            $entityManager->flush();
        }

        return $this->render('dashboard/dashboard-add-property.html.twig', [
            'form' => $form->createView(),
        ]);

    }

Options

License MIT