<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Contracts\Translation\TranslatorInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use App\Entity\Donations;
#use Symfony\Component\Form\FormEvent;
#use Symfony\Component\Form\FormEvents;
Class DonationsFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('userid', HiddenType::Class)
->add('amount', TextType::Class, [
'constraints' => [
new Assert\NotBlank(),
new Assert\Length(['min' => 1, 'max' => 5]),
],
'data' => isset($options['data']) ? $options['data']->getAmount() : '5'
])
;
/*$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$donation = $event->getData();
$form = $event->getForm();
if (!$donation || null === $donation->getId()) {
$form->add('sidebar', HiddenType::Class, [
'data' => '1',
'required' => true
]);
}
});*/
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => Donations::Class
]);
}
}