1 <?php
declare(strict_types
=1);
4 * This file is part of the Rapsys AirBundle package.
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Rapsys\AirBundle\Form
;
14 use Symfony\Component\Form\AbstractType
;
15 use Symfony\Component\Form\Extension\Core\Type\HiddenType
;
16 use Symfony\Component\Form\Extension\Core\Type\FileType
;
17 use Symfony\Component\Form\Extension\Core\Type\SubmitType
;
18 use Symfony\Component\Form\FormBuilderInterface
;
19 use Symfony\Component\OptionsResolver\OptionsResolver
;
20 use Symfony\Component\Validator\Constraints\File
;
25 class ImageType
extends AbstractType
{
29 public function buildForm(FormBuilderInterface
$form, array $options): FormBuilderInterface
{
31 if ($options['location']) {
32 $form->add('location', HiddenType
::class, ['required' => true]);
36 if ($options['user']) {
37 $form->add('user', HiddenType
::class, ['required' => true]);
41 if ($options['image']) {
43 $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]);
47 $form->add('submit', SubmitType
::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
50 if ($options['delete']) {
52 //TODO: add confirm on click ?
53 $form->add('delete', SubmitType
::class, ['label' => 'Delete', 'attr' => ['class' => 'submit']]);
63 public function configureOptions(OptionsResolver
$resolver): void {
65 $resolver->setDefaults(['delete' => true, 'error_bubbling' => true, 'image' => true, 'location' => true, 'user' => true]);
67 //Add extra delete option
68 $resolver->setAllowedTypes('delete', 'boolean');
70 //Add extra image option
71 $resolver->setAllowedTypes('image', 'boolean');
73 //Add extra location option
74 $resolver->setAllowedTypes('location', 'boolean');
76 //Add extra user option
77 $resolver->setAllowedTypes('user', 'boolean');
83 public function getName(): string {
84 return 'rapsysair_image';