From: Raphaël Gertz Date: Thu, 13 Oct 2022 12:14:48 +0000 (+0200) Subject: Add image form type X-Git-Tag: 0.3.0~200 X-Git-Url: https://git.rapsys.eu/airbundle/commitdiff_plain/c1b4f0a7f8d6bbbc69fd3f7e1587638d5ea45ad2 Add image form type --- diff --git a/Form/ImageType.php b/Form/ImageType.php new file mode 100644 index 0000000..d1f7de0 --- /dev/null +++ b/Form/ImageType.php @@ -0,0 +1,86 @@ + + * + * 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'; + } +}