src/Form/SupplierInvoicesType.php line 19
<?php
namespace App\Form;
use App\Entity\SupplierInvoices;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Contracts\Translation\TranslatorInterface;
class SupplierInvoicesType extends AbstractType
{
/**
* @var Translator
*/
protected $translator;
/**
* VehicleBeneficiaryDetailsType constructor.
* @param Translator|null $translator
*/
public function __construct(TranslatorInterface $translator= null)
{
$this->translator = $translator;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$trans = $this->translator;
$numberValidation = $trans->trans('you should enter numbers', [], 'validators');
$alphanumericValidation = $trans->trans('you should enter alpha-numeric', [], 'validators');
$builder->add('total', null, array(
'label' => 'total',
'required' => false,
'attr' => ['validate-msg' => $numberValidation]
))
->add('invoiceNumber', null, array(
'label' => 'Claim Invoice Number',
'required' => false,
'attr' => ['validate-msg' => $alphanumericValidation]
))
->add('isPayed', CheckboxType::class, array(
'label' => 'Payment Status',
'required' => false
))
->add('paymentType', ChoiceType::class, array(
'label' => 'Payment Method',
'choices' => SupplierInvoices::PAYMENT_TYPES,
'required' => false,
'placeholder' => 'Please Select',
))
->add('paymentDate', DateType::class, array(
'label' => 'Invoice Date',
'widget' => 'single_text',
'required' => false,
'html5' => false
))
->add('imagePath', FileType::class, array(
'label' => 'Attach an Image',
'required' => false,
'attr' => ['class' => 'docImg']
))
->add('clearImage', HiddenType::class, array(
'label' => false,
'required' => false,
'mapped' => false
));
//show the checkbox of including/ignoring VAT in invoice value only if there is an effective VAT rate
//this is the way to show the form field if the form page was reloaded due to failed validation
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) use ($options ) {
$invoice = $event->getData();
$form = $event->getForm();
$shallShowVatCheckbox= false;
if( !$invoice && $options['theEffectiveVat']) {
//if no invoice yet and there is an Effective VAT rate then show the option of adding/ignoring VAT
$shallShowVatCheckbox = true;
$vatText = $options['theEffectiveVat']->getVatText();
}elseif ($invoice && $invoice->getVatRate()){
//else if there is an invoice and it has a VAT rate then show the option of adding/ignoring VAT
$shallShowVatCheckbox = true;
$vatText = $invoice->getVatRate()->getVatText();
}
if($shallShowVatCheckbox) {
$form->add('check_vat_amount', CheckboxType::class, array(
'label' => 'value is vat inclusive',
'label_translation_parameters' => [
'%vat_rate%' => $vatText],
'required' => false,
'attr' => array('class' => ''),
'mapped' => false
));
}
});
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'App\Entity\SupplierInvoices',
'translation_domain' => 'vehicles',
'locale' => 'en',
'allow_extra_fields' => true,
'theEffectiveVat' => false
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'appbundle_supplierinvoices';
}
}