]> Raphaël G. Git Repositories - airbundle/commitdiff
Add image form type
authorRaphaël Gertz <git@rapsys.eu>
Thu, 13 Oct 2022 12:14:48 +0000 (14:14 +0200)
committerRaphaël Gertz <git@rapsys.eu>
Thu, 13 Oct 2022 12:14:48 +0000 (14:14 +0200)
Form/ImageType.php [new file with mode: 0644]

diff --git a/Form/ImageType.php b/Form/ImageType.php
new file mode 100644 (file)
index 0000000..d1f7de0
--- /dev/null
@@ -0,0 +1,86 @@
+<?php declare(strict_types=1);
+
+/*
+ * This file is part of the Rapsys AirBundle package.
+ *
+ * (c) Raphaël Gertz <symfony@rapsys.eu>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Rapsys\AirBundle\Form;
+
+use Symfony\Component\Form\AbstractType;
+use Symfony\Component\Form\Extension\Core\Type\HiddenType;
+use Symfony\Component\Form\Extension\Core\Type\FileType;
+use Symfony\Component\Form\Extension\Core\Type\SubmitType;
+use Symfony\Component\Form\FormBuilderInterface;
+use Symfony\Component\OptionsResolver\OptionsResolver;
+use Symfony\Component\Validator\Constraints\File;
+
+/**
+ * {@inheritdoc}
+ */
+class ImageType extends AbstractType {
+       /**
+        * {@inheritdoc}
+        */
+       public function buildForm(FormBuilderInterface $form, array $options): FormBuilderInterface {
+               //With location
+               if ($options['location']) {
+                       $form->add('location', HiddenType::class, ['required' => true]);
+               }
+
+               //With user
+               if ($options['user']) {
+                       $form->add('user', HiddenType::class, ['required' => true]);
+               }
+
+               //With image
+               if ($options['image']) {
+                       //With image
+                       $form->add('image', FileType::class, ['attr' => ['placeholder' => 'Your image'], 'constraints' => [new File(['maxSize' => '5M', 'mimeTypes' => ['image/jpeg', 'image/png', 'image/tiff', 'image/webp'], 'mimeTypesMessage' => 'Please upload a valid Image document'])]/*, 'mapped' => false*/, 'required' => $options['delete'] ? false : true]);
+               }
+
+               //Add submit
+               $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
+
+               //With delete
+               if ($options['delete']) {
+                       //Add delete
+                       //TODO: add confirm on click ?
+                       $form->add('delete', SubmitType::class, ['label' => 'Delete', 'attr' => ['class' => 'submit']]);
+               }
+
+               //Return form builder
+               return $form;
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function configureOptions(OptionsResolver $resolver): void {
+               //Set defaults
+               $resolver->setDefaults(['delete' => true, 'error_bubbling' => true, 'image' => true, 'location' => true, 'user' => true]);
+
+               //Add extra delete option
+               $resolver->setAllowedTypes('delete', 'boolean');
+
+               //Add extra image option
+               $resolver->setAllowedTypes('image', 'boolean');
+
+               //Add extra location option
+               $resolver->setAllowedTypes('location', 'boolean');
+
+               //Add extra user option
+               $resolver->setAllowedTypes('user', 'boolean');
+       }
+
+       /**
+        * {@inheritdoc}
+        */
+       public function getName(): string {
+               return 'rapsys_air_image';
+       }
+}