<?php
namespace App\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
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\SubmitButton;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Blank;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Collection;
class ContactType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$subject = "";
if(isset ($options->data['subject']))
{
$subject = $options->data['subject'];
}
$builder->add('name', TextType::class, array(
'attr' => array(
'pattern' => '.{2,}'
)
))
->add('email', EmailType::class, array(
'label' => 'E-Mail',
))
->add('subject', TextType::class, array(
'label' => 'Betreff',
'attr' => array(
'pattern' => '.{3,}',
'label' => $subject
)
))
->add('content', TextAreaType::class, array(
'label' => 'Nachricht',
'attr' => array(
'cols' => 50,
'rows' => 10)
))
->add('spam_content', TextType::class, array('label' => false ) )// bot protection!
->add('send', SubmitType::class, array('label' => 'Senden') )
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$collectionConstraint = new Collection(array(
'name' => array(
new NotBlank(array('message' => 'Bitte ausfüllen.')),
new Length(array('min' => 2))
),
'email' => array(
new NotBlank(array('message' => 'Bitte ausfüllen.')),
new Email(array('message' => 'Ungültige Adresse.'))
),
'subject' => array(
new NotBlank(array('message' => 'Bitte ausfüllen.')),
new Length(array('min' => 3))
),
'content' => array(
),
'spam_content' => array(
new NotBlank(array('message' => 'Bitte ausfüllen.')),
new Length(array('min' => 5))
)
));
$resolver->setDefaults(array(
'constraints' => $collectionConstraint
));
}
public function getName()
{
return 'contact';
}
}