<?php
namespace App\Form\Ecommerce;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* Class AddressShippableType.
*/
class AddressShippableType extends AbstractType
{
private $class;
/**
* @param string $class
*/
public function __construct($class)
{
$this->class = $class;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
if ($options['same_as_billing']) {
$builder
->add('sameAsBilling', CheckboxType::class, [
'required' => false,
'mapped' => false,
'label' => 'ecommerce.label.address.same_as_billing',
])
;
}
$builder
->add('name', null, ['label' => 'form.global.label_name', 'required' => true])
->add('surname', null, ['label' => 'form.global.label_surname', 'required' => true])
->add('nif', HiddenType::class, ['label' => 'form.global.label_nif', 'data' => 'ssss', 'empty_data' => 'ssss'])
->add('email', EmailType::class, ['label' => 'form.global.label_email', 'required' => true])
->add('phone', PhoneNumberType::class, ['widget' => PhoneNumberType::WIDGET_COUNTRY_CHOICE, 'preferred_country_choices' => ['ES'], 'required' => true])
->add('addressLine1', null, ['label' => 'form.global.label_address', 'required' => true])
->add(
'city',
null,
[
'label' => 'ecommerce.label.address.city',
'required' => true,
]
)
->add('postalCode', TextType::class, [
'label' => 'ecommerce.label.address.postalCode',
'required' => true,
])
->add('country', CountryType::class, [
'label' => 'ecommerce.label.address.country',
'preferred_choices' => ['ES'],
'required' => true,
])
->add('comment', TextareaType::class, [
'label' => 'form.global.label_comment',
])
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => $this->class,
'validation_groups' => function (FormInterface $form) {
if ($form->has('sameAsBilling') && $form->get('sameAsBilling')->getData()) {
return [];
}
return ['Checkout'];
},
'csrf_protection' => false,
'allow_extra_fields' => true,
'same_as_billing' => false,
]);
}
/**
* {@inheritdoc}
*/
public function getName()
{
return 'wam_ecommerce_addressshippable';
}
}