<?php
namespace App\Form\Type;
use App\Entity\Cabecera;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\Type;
class CabeceraType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
// ->add('nombrecliente', TextType::class)
->add('nombres', TextType::class)
->add('apellidos', TextType::class, ['required' => false])
->add('sucursal', HiddenType::class)
->add('direccioncliente', TextType::class)
->add('direccion2cliente', TextType::class, ['required' => false])
->add('tipodeservicio', ChoiceType::class, [
'choices' => Cabecera::SERVICE_TYPE,
'expanded' => true,
'multiple' => false,
])
->add('telefonocliente', TextType::class, [
'constraints' => [
new Type([
'type' => 'numeric',
'message' => 'El teléfono debe ser numérico',
]),
],
])
->add('nifcliente', TextType::class, [
'required' => false,
'constraints' => [
new Type([
'type' => 'numeric',
'message' => 'El documento debe ser numérico',
]),
],
])
->add('emailcliente', EmailType::class, ['required' => false])
// ->add('facturaelectronica', CheckboxType::class, [
// 'label' => 'Factura Electrónica',
// 'required' => false,
// ])
->add('nombre_receptor', TextType::class, ['required' => false, 'label' => 'Nombre del que recibe'])
->add('telefono_receptor', TextType::class, ['required' => false, 'label' => 'telefono del que recibe'])
// ->add('observaciones_receptor', TextareaType::class, ['required' => false, 'label' => 'observaciones'])
->add('save', SubmitType::class, ['label' => 'Continuar']);
if (!$options['editable_mode']) {
$builder->get('nombres')->setDisabled(true);
$builder->get('apellidos')->setDisabled(true);
$builder->get('nifcliente')->setDisabled(true);
$builder->get('emailcliente')->setDisabled(true);
$builder->get('telefonocliente')->setDisabled(true);
}
// else {
// $builder->remove('facturaelectronica');
// }
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$cab = $event->getData();
$form = $event->getForm();
if (null === $cab->getFechareserva()) {
$form->add('fechareserva', DateTimeType::class, [
'date_widget' => 'single_text',
'data' => new \DateTime(),
'hours' => range(7, 22),
'minutes' => array(00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55),
]);
} else {
$form->add('fechareserva', DateTimeType::class, [
'date_widget' => 'single_text',
'hours' => range(7, 22),
'minutes' => array(00, 05, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55),
]);
}
});
// $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
// $cab = $event->getData();
// $form = $event->getForm();
// // checks if the Product object is "new"
// // If no data is passed to the form, the data is "null".
// // This should be considered a new "Product"
// if (null === $cab->getFechareserva()) {
// $form->add('name', TextType::class);
// }
// });
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => Cabecera::class,
'editable_mode' => true,
]);
}
}