From: Raphaƫl Gertz Date: Thu, 13 Oct 2022 12:25:07 +0000 (+0200) Subject: Convert every optional field as toggleable X-Git-Tag: 0.3.0~199 X-Git-Url: https://git.rapsys.eu/airbundle/commitdiff_plain/3d104470142fb817f18fbf536a19b65bbdad07b5?ds=sidebyside;hp=c1b4f0a7f8d6bbbc69fd3f7e1587638d5ea45ad2 Convert every optional field as toggleable Drop useless notblank constraint Php strict Cleanup --- diff --git a/Form/SnippetType.php b/Form/SnippetType.php index 05f5818..0bd8307 100644 --- a/Form/SnippetType.php +++ b/Form/SnippetType.php @@ -13,51 +13,113 @@ use Symfony\Component\Form\Extension\Core\Type\UrlType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Validator\Constraints\File; -use Symfony\Component\Validator\Constraints\NotBlank; use Rapsys\AirBundle\Form\Extension\Type\HiddenEntityType; -use Rapsys\AirBundle\Entity\Location; -use Rapsys\AirBundle\Entity\User; use Rapsys\AirBundle\Entity\Snippet; class SnippetType extends AbstractType { /** * {@inheritdoc} */ - public function buildForm(FormBuilderInterface $builder, array $options) { - return $builder + public function buildForm(FormBuilderInterface $form, array $options): FormBuilderInterface { + //Start build form + $form + //Add locale, location and user hidden fields ->add('locale', HiddenType::class, ['required' => true]) ->add('location', HiddenEntityType::class, ['required' => true]) - ->add('user', HiddenEntityType::class, ['required' => true]) - ->add('description', TextareaType::class, ['attr' => ['placeholder' => 'Your description', 'cols' => 50, 'rows' => 15], 'required' => false]) - ->add('class', TextareaType::class, ['attr' => ['placeholder' => 'Your class', 'cols' => 50, 'rows' => 10], 'required' => false]) - ->add('short', TextareaType::class, ['attr' => ['placeholder' => 'Your short', 'cols' => 50, 'rows' => 10], 'required' => false]) - ->add('rate', NumberType::class, ['attr' => ['placeholder' => 'Your rate'], 'required' => false]) - ->add('hat', CheckboxType::class, ['attr' => ['placeholder' => 'Your hat'], 'required' => false]) - ->add('contact', UrlType::class, ['attr' => ['placeholder' => 'Your contact'], 'required' => false]) - ->add('donate', UrlType::class, ['attr' => ['placeholder' => 'Your donate'], 'required' => false]) - ->add('link', UrlType::class, ['attr' => ['placeholder' => 'Your link'], 'required' => false]) - ->add('profile', UrlType::class, ['attr' => ['placeholder' => 'Your profile'], 'required' => false]) - ->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' => false]) - ->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]); + ->add('user', HiddenEntityType::class, ['required' => true]); + + //With description + if ($options['description']) { + $form->add('description', TextareaType::class, ['attr' => ['placeholder' => 'Your description', 'cols' => 50, 'rows' => 15], 'required' => false]); + } + + //With class + if ($options['class']) { + $form->add('class', TextareaType::class, ['attr' => ['placeholder' => 'Your class', 'cols' => 50, 'rows' => 10], 'required' => false]); + } + + //With short + if ($options['short']) { + $form->add('short', TextareaType::class, ['attr' => ['placeholder' => 'Your short', 'cols' => 50, 'rows' => 10], 'required' => false]); + } + + //With rate + if ($options['rate']) { + $form->add('rate', NumberType::class, ['attr' => ['placeholder' => 'Your rate'], 'required' => false]); + } + + //With hat + if ($options['hat']) { + $form->add('hat', CheckboxType::class, ['attr' => ['placeholder' => 'Your hat'], 'required' => false]); + } + + //With contact + if ($options['contact']) { + $form->add('contact', UrlType::class, ['attr' => ['placeholder' => 'Your contact'], 'required' => false]); + } + + //With donate + if ($options['donate']) { + $form->add('donate', UrlType::class, ['attr' => ['placeholder' => 'Your donate'], 'required' => false]); + } + + //With link + if ($options['link']) { + $form->add('link', UrlType::class, ['attr' => ['placeholder' => 'Your link'], 'required' => false]); + } + + //With profile + if ($options['profile']) { + $form->add('profile', UrlType::class, ['attr' => ['placeholder' => 'Your profile'], 'required' => false]); + } + + //Add submit + $form->add('submit', SubmitType::class, ['label' => 'Send', 'attr' => ['class' => 'submit']]); + + //Return form builder + return $form; } /** * {@inheritdoc} */ - public function configureOptions(OptionsResolver $resolver) { - $resolver->setDefaults(['data_class' => Snippet::class, 'error_bubbling' => true, 'location' => null, 'user' => null]); - $resolver->setAllowedTypes('location', [Location::class, 'null']); - $resolver->setAllowedTypes('user', [User::class, 'null']); + public function configureOptions(OptionsResolver $resolver): void { + //Set defaults + $resolver->setDefaults(['class' => true, 'contact' => true, 'data_class' => Snippet::class, 'description' => true, 'donate' => true, 'error_bubbling' => true, 'hat' => true, 'link' => true, 'profile' => true, 'rate' => true, 'short' => true]); + + //Add extra class option + $resolver->setAllowedTypes('class', 'boolean'); + + //Add extra contact option + $resolver->setAllowedTypes('contact', 'boolean'); + + //Add extra description option + $resolver->setAllowedTypes('description', 'boolean'); + + //Add extra donate option + $resolver->setAllowedTypes('donate', 'boolean'); + + //Add extra hat option + $resolver->setAllowedTypes('hat', 'boolean'); + + //Add extra link option + $resolver->setAllowedTypes('link', 'boolean'); + + //Add extra profile option + $resolver->setAllowedTypes('profile', 'boolean'); + + //Add extra rate option + $resolver->setAllowedTypes('rate', 'boolean'); + + //Add extra short option + $resolver->setAllowedTypes('short', 'boolean'); } /** * {@inheritdoc} - * XXX: this doesn't work, because it's impossible to generate this same id on other side - * TODO: we would need to be able to generate this id at form creation - * - public function getBlockPrefix() { - //Prevent collision between instances with an unique block prefix - return 'snippet_'.uniqid(); - }*/ + */ + public function getName(): string { + return 'rapsys_air_snippet'; + } }