]> Raphaël G. Git Repositories - airbundle/blob - Form/ImageType.php
Add site.donate entry, facebook and path trees
[airbundle] / Form / ImageType.php
1 <?php declare(strict_types=1);
2
3 /*
4 * This file is part of the Rapsys AirBundle package.
5 *
6 * (c) Raphaël Gertz <symfony@rapsys.eu>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Rapsys\AirBundle\Form;
13
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;
21
22 /**
23 * {@inheritdoc}
24 */
25 class ImageType extends AbstractType {
26 /**
27 * {@inheritdoc}
28 */
29 public function buildForm(FormBuilderInterface $form, array $options): FormBuilderInterface {
30 //With location
31 if ($options['location']) {
32 $form->add('location', HiddenType::class, ['required' => true]);
33 }
34
35 //With user
36 if ($options['user']) {
37 $form->add('user', HiddenType::class, ['required' => true]);
38 }
39
40 //With image
41 if ($options['image']) {
42 //With 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]);
44 }
45
46 //Add submit
47 $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]);
48
49 //With delete
50 if ($options['delete']) {
51 //Add delete
52 //TODO: add confirm on click ?
53 $form->add('delete', SubmitType::class, ['label' => 'Delete', 'attr' => ['class' => 'submit']]);
54 }
55
56 //Return form builder
57 return $form;
58 }
59
60 /**
61 * {@inheritdoc}
62 */
63 public function configureOptions(OptionsResolver $resolver): void {
64 //Set defaults
65 $resolver->setDefaults(['delete' => true, 'error_bubbling' => true, 'image' => true, 'location' => true, 'user' => true]);
66
67 //Add extra delete option
68 $resolver->setAllowedTypes('delete', 'boolean');
69
70 //Add extra image option
71 $resolver->setAllowedTypes('image', 'boolean');
72
73 //Add extra location option
74 $resolver->setAllowedTypes('location', 'boolean');
75
76 //Add extra user option
77 $resolver->setAllowedTypes('user', 'boolean');
78 }
79
80 /**
81 * {@inheritdoc}
82 */
83 public function getName(): string {
84 return 'rapsys_air_image';
85 }
86 }