<?php
namespace App\Form;
use App\Entity\Farm;
use App\Entity\Producer;
use App\Entity\User;
use App\Entity\Organization;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Contracts\Translation\TranslatorInterface;
class FarmType extends AbstractType
{
/**
* @var Security
*/
private $router;
private TranslatorInterface $translator;
private Security $security;
private User $user;
private ?Organization $organization;
public function __construct(
Security $security,
TranslatorInterface $translator,
RouterInterface $router)
{
$this->router = $router;
$this->translator = $translator;
$this->security = $security;
$this->user = $security->getUser() ?? new User();
$this->organization = $this->user->getOrganization();
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$_ = $this->translator;
if ($this->user->isAdmin()) {
$builder->add('organization', EntityType::class, [
'class' => Organization::class,
'attr' => ['class' => 'filters selectize','placeholder' => 'select.organization'],
'required' => true,
'label' => 'Form.organizationLabel',
'placeholder' => $_->trans('select.organization')
]);
}
$builder
->add('name', null, [
'attr' => [
'autocomplete' => 'off',
'placeholder' => 'select.name',
],
'label' => 'Form.farmLabel',
'required' => true,
])
->add('cottage', null, [
'label' => 'Farm.cottage'
])
->add('raise', null, [
'label' => 'Farm.raise'
])
->add('feedlot', null, [
'label' => 'Farm.feedlot',
])
->add('wintering', null, [
'label' => 'Farm.wintering'
])
->add('mix', null, [
'label' => 'Farm.mix'
])
->add('tambo', null, [
'label' => 'Farm.tambo'
])
->add('other', null, [
'label' => 'Farm.other'
])
->add('cuartel', null, [
'label' => 'Farm.cuartel'
])
->add('picture', FileType ::class, [
'label' => 'Imagen',
'mapped' => false,
'required' => false,
'error_bubbling' => true,
'constraints' => [
new Image([
'maxSize' => '2M',
'mimeTypes' => [
'image/png',
'image/jpg',
'image/jpeg',
],
'mimeTypesMessage' => 'file.image.error', // traduccion en validators.yaml
])
]
])
->add('tenure')
->add('cuig', null, [
'attr' => [
'class' => 'form-control',
'placeholder' => 'AB001',
'autocomplete' => 'off',
'maxlength' => 5,
'minlength' => 5,
'pattern' => '[a-zA-Z]{2}\d{3}'
],
'required' => true,
'constraints' => [
new NotBlank(),
new Regex('/[a-zA-Z]{2}\d{3}/')
],
'validation_groups' => ['valid-value'],
])
->add('hectares', NumberType::class, [
'attr' => [
'data-cleave' => "{numeral: true, numeralDecimalMark: ',', delimiter: '.'}",
'style' => 'text-align:left',
'placeholder' => 'select.hectares',
],
'required' => false,
])
->add('timeStamp')
->add('farmProducers', EntityType::class, [ /*{{{*/
'label' => 'Form.farmProducersLabel',
'multiple' => true,
'attr' => ['class' => 'filters selectize','placeholder' => 'select.farmProducer'],
'class' => Producer::class,
'query_builder' => function ( $repository ) { return $repository->entryHelp( $this->organization ); },
'validation_groups' => ['valid-value'],
])/*}}}*/
->add('producer', EntityType::class, [ /*{{{*/
'label' => 'Form.defaultProducerLabel',
'attr' => ['class' => 'filters selectize','placeholder' => 'select.defaultProducer'],
'class' => Producer::class,
'query_builder' => function ( $repository ) { return $repository->entryHelp( $this->organization ); },
'validation_groups' => ['valid-value'], ])/*}}}*/
->add('renspa', TextType::class, [
'attr' => [
'data-cleave' => json_encode(['blocks' => [2, 3, 1, 5, 2], 'delimiter' => '.']),
'class' => 'form-control',
'placeholder' => '01.001.1.10101/01',
'autocomplete' => 'off',
'maxlength' => 17,
'minlength' => 17,
],
// 'required => true,
'constraints' => [
new Regex('/\d{2}\.\d{3}\.\d{1}\.\d{5}\.\d{2}/'),
],
'validation_groups' => ['valid-value'],
])
->add('address', TextType::class, [
'required' => false,
'attr' => [
'data-selectize'=>'{"valueField":"id","labelField":"name", "searchField":"name", "delimiter": "|"}',
'class' => 'filters selectize geocode','placeholder' => $_->trans('select.addressChoice')],
'label' => 'Form.addressLabel',
])
->add('activity', null, [
'label' => 'Form.activityLabel',
'attr' => [ 'placeholder' => 'select.activity']
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Farm::class,
'validation_groups' => ['valid-value'],
]);
}
}