src/Form/PostType.php line 18

  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Post;
  4. use DateTime;
  5. use Symfony\Component\Form\AbstractType;
  6. use Symfony\Component\Form\Extension\Core\Type\FileType;
  7. use Symfony\Component\Validator\Constraints\File;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\OptionsResolver\OptionsResolver;
  10. use Symfony\Contracts\Translation\TranslatorInterface;
  11. use Symfony\Component\Form\Extension\Core\Type\DateType;
  12. use Symfony\Component\Form\Extension\Core\Type\TextType;
  13. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  14. use Symfony\Component\Form\Extension\Core\Type\CollectionType;
  15. class PostType extends AbstractType
  16. {
  17.     public function __construct(private TranslatorInterface $translator)
  18.     {
  19.     }
  20.     public function buildForm(FormBuilderInterface $builder, array $options): void
  21.     {
  22.         $builder
  23.             ->add('name'TextType::class, [
  24.                 'label' => $this->translator->trans('Titre'),
  25.                 'required' => true,
  26.                 'attr' => [
  27.                     'class' => 'form-control',
  28.                     'placeholder' => ''
  29.                 ],
  30.                 'help' => ''
  31.             ])
  32.             ->add('description'TextareaType::class, [
  33.                 'label' => $this->translator->trans('Description'),
  34.                 'required' => false,
  35.                 'attr' => [
  36.                     'class' => 'form-control',
  37.                     'placeholder' => ''
  38.                 ],
  39.                 'help' => ''
  40.             ])
  41.             ->add('date'DateType::class, [
  42.                 'attr' => [
  43.                     'class' => 'form-control',
  44.                     'placeholder' => ''
  45.                 ],
  46.                 'label' => $this->translator->trans('Date'),
  47.                 'widget' => 'single_text',
  48.                 'data' => new DateTime()
  49.             ])
  50.             ->add('tags'TextType::class, [
  51.                 'label' => $this->translator->trans('Mots clés'),
  52.                 'attr' => [
  53.                     'class' => 'select-tags select2-modal'
  54.                 ],
  55.                 'required' => false
  56.             ])
  57.             ->add('photo'FileType::class, [
  58.                 'attr' => [
  59.                     'class' => 'form-control',
  60.                     'placeholder' => ''
  61.                 ],
  62.                 'label' => $this->translator->trans('Photos'),
  63.                 'mapped' => false,
  64.                 'multiple' => true,
  65.                 'required' => false,
  66.                 'constraints' => [
  67.                     new File([
  68.                         'maxSize' => '2048k',
  69.                         'mimeTypes' => [
  70.                             'image/*',
  71.                         ],
  72.                         'mimeTypesMessage' => $this->translator->trans('Veuillez télécharger une image valide'),
  73.                     ])
  74.                 ],
  75.             ])
  76.         ;
  77.     }
  78.     public function configureOptions(OptionsResolver $resolver): void
  79.     {
  80.         $resolver->setDefaults([
  81.             'data_class' => Post::class,
  82.         ]);
  83.     }
  84. }