src/Form/FarmType.php line 147

Open in your IDE?
  1. <?php
  2. namespace App\Form;
  3. use App\Entity\Farm;
  4. use App\Entity\Producer;
  5. use App\Entity\User;
  6. use App\Entity\Organization;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\Form\AbstractType;
  9. use Symfony\Component\Form\Extension\Core\Type\FileType;
  10. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  11. use Symfony\Component\Form\Extension\Core\Type\TextType;
  12. use Symfony\Component\Form\FormBuilderInterface;
  13. use Symfony\Component\OptionsResolver\OptionsResolver;
  14. use Symfony\Component\Routing\RouterInterface;
  15. use Symfony\Component\Security\Core\Security;
  16. use Symfony\Component\Validator\Constraints\Image;
  17. use Symfony\Component\Validator\Constraints\NotBlank;
  18. use Symfony\Component\Validator\Constraints\Regex;
  19. use Symfony\Contracts\Translation\TranslatorInterface;
  20. class FarmType extends AbstractType
  21. {
  22.     /**
  23.      * @var Security
  24.      */
  25.     private $router;
  26.     private TranslatorInterface $translator;
  27.     private Security $security;
  28.     private User $user;
  29.     private ?Organization $organization;
  30.     public function __construct(
  31.         Security $security
  32.         TranslatorInterface $translator
  33.         RouterInterface $router)
  34.     {
  35.         $this->router $router;
  36.         $this->translator $translator;
  37.         $this->security $security;
  38.         $this->user $security->getUser() ?? new User();
  39.         $this->organization $this->user->getOrganization();
  40.     }
  41.     public function buildForm(FormBuilderInterface $builder, array $options)
  42.     {
  43.         $_ $this->translator;
  44.         if ($this->user->isAdmin()) {
  45.             $builder->add('organization'EntityType::class, [
  46.                 'class' => Organization::class,
  47.                 'attr' => ['class' => 'filters selectize','placeholder' => 'select.organization'],
  48.                 'required' => true,
  49.                 'label' => 'Form.organizationLabel',
  50.                 'placeholder' => $_->trans('select.organization')
  51.             ]);
  52.         }
  53.         $builder
  54.             ->add('name'null, [
  55.                 'attr' => [
  56.                     'autocomplete' => 'off',
  57.                     'placeholder' => 'select.name',
  58.                 ],
  59.                 'label' => 'Form.farmLabel',
  60.                 'required' => true,
  61.             ])
  62.             ->add('cottage'null, [
  63.                 'label' => 'Farm.cottage'
  64.             ])
  65.             ->add('raise'null, [
  66.                 'label' => 'Farm.raise'
  67.             ])
  68.             ->add('feedlot'null, [
  69.                 'label' => 'Farm.feedlot',
  70.             ])
  71.             ->add('wintering'null, [
  72.                 'label' => 'Farm.wintering'
  73.             ])
  74.             ->add('mix'null, [
  75.                 'label' => 'Farm.mix'
  76.             ])
  77.             ->add('tambo'null, [
  78.                 'label' => 'Farm.tambo'
  79.             ])
  80.             ->add('other'null, [
  81.                 'label' => 'Farm.other'
  82.             ])
  83.             ->add('cuartel'null, [
  84.                 'label' => 'Farm.cuartel'
  85.             ])
  86.             ->add('picture'FileType   ::class, [
  87.                 'label' => 'Imagen',
  88.                 'mapped' => false,
  89.                 'required' => false,
  90.                 'error_bubbling' => true,
  91.                 'constraints' => [
  92.                     new Image([
  93.                         'maxSize' => '2M',
  94.                         'mimeTypes' => [
  95.                             'image/png',
  96.                             'image/jpg',
  97.                             'image/jpeg',
  98.                         ],
  99.                         'mimeTypesMessage' => 'file.image.error'// traduccion en validators.yaml
  100.                     ])
  101.                 ]
  102.             ])
  103.             ->add('tenure')
  104.             ->add('cuig'null, [
  105.                 'attr' => [
  106.                     'class' => 'form-control',
  107.                     'placeholder' => 'AB001',
  108.                     'autocomplete' => 'off',
  109.                     'maxlength' => 5,
  110.                     'minlength' => 5,
  111.                     'pattern' => '[a-zA-Z]{2}\d{3}'
  112.                 ],
  113.                 'required' => true,
  114.                 'constraints' => [
  115.                     new NotBlank(),
  116.                     new Regex('/[a-zA-Z]{2}\d{3}/')
  117.                 ],
  118.                 'validation_groups' => ['valid-value'],
  119.             ])
  120.             ->add('hectares'NumberType::class, [
  121.                 'attr' => [
  122.                     'data-cleave' => "{numeral: true, numeralDecimalMark: ',', delimiter: '.'}",
  123.                     'style' => 'text-align:left',
  124.                     'placeholder' => 'select.hectares',
  125.                 ],
  126.                 'required' => false,
  127.             ])
  128.             ->add('timeStamp')
  129.             ->add('farmProducers'EntityType::class, [ /*{{{*/
  130.                 'label' => 'Form.farmProducersLabel',
  131.                 'multiple' => true,
  132.                 'attr' => ['class' => 'filters selectize','placeholder' => 'select.farmProducer'],
  133.                 'class' => Producer::class,
  134.                 'query_builder' => function ( $repository ) { return $repository->entryHelp$this->organization ); },
  135.                 'validation_groups' => ['valid-value'],
  136.             ])/*}}}*/
  137.             ->add('producer'EntityType::class, [ /*{{{*/
  138.                 'label' => 'Form.defaultProducerLabel',
  139.                 'attr' => ['class' => 'filters selectize','placeholder' => 'select.defaultProducer'],
  140.                 'class' => Producer::class,
  141.                 'query_builder' => function ( $repository ) { return $repository->entryHelp$this->organization ); },
  142.                 'validation_groups' => ['valid-value'], ])/*}}}*/
  143.             ->add('renspa'TextType::class, [
  144.                 'attr' => [
  145.                     'data-cleave' => json_encode(['blocks' => [2315,  2], 'delimiter' => '.']),
  146.                     'class' => 'form-control',
  147.                     'placeholder' => '01.001.1.10101/01',
  148.                     'autocomplete' => 'off',
  149.                     'maxlength' => 17,
  150.                     'minlength' => 17,
  151.                 ],
  152.                 // 'required => true,
  153.                 'constraints' => [
  154.                     new Regex('/\d{2}\.\d{3}\.\d{1}\.\d{5}\.\d{2}/'),
  155.                 ],
  156.                 'validation_groups' => ['valid-value'],
  157.             ])
  158.             ->add('address'TextType::class, [
  159.                 'required' => false,
  160.                 'attr' => [
  161.                     'data-selectize'=>'{"valueField":"id","labelField":"name", "searchField":"name", "delimiter": "|"}',
  162.                     'class' => 'filters selectize geocode','placeholder' => $_->trans('select.addressChoice')],
  163.                 'label' => 'Form.addressLabel',
  164.             ])
  165.             ->add('activity'null, [
  166.                 'label' => 'Form.activityLabel',
  167.                 'attr' => [ 'placeholder' => 'select.activity']
  168.             ])
  169.         ;
  170.     }
  171.     public function configureOptions(OptionsResolver $resolver)
  172.     {
  173.         $resolver->setDefaults([
  174.             'data_class' => Farm::class,
  175.             'validation_groups' => ['valid-value'],
  176.         ]);
  177.     }
  178. }