src/Form/Type/ContactType.php line 19

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type;
  3. use Symfony\Component\Form\AbstractType;
  4. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  5. use Symfony\Component\Form\Extension\Core\Type\SubmitType;
  6. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  7. use Symfony\Component\Form\Extension\Core\Type\TextType;
  8. use Symfony\Component\Form\FormBuilderInterface;
  9. use Symfony\Component\Form\SubmitButton;
  10. use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  11. use Symfony\Component\Validator\Constraints\Email;
  12. use Symfony\Component\Validator\Constraints\Length;
  13. use Symfony\Component\Validator\Constraints\Blank;
  14. use Symfony\Component\Validator\Constraints\NotBlank;
  15. use Symfony\Component\Validator\Constraints\Collection;
  16. class ContactType extends AbstractType
  17. {
  18. public function buildForm(FormBuilderInterface $builder, array $options)
  19. {
  20. $subject = "";
  21. if(isset ($options->data['subject']))
  22. {
  23. $subject = $options->data['subject'];
  24. }
  25. $builder->add('name', TextType::class, array(
  26. 'attr' => array(
  27. 'pattern' => '.{2,}'
  28. )
  29. ))
  30. ->add('email', EmailType::class, array(
  31. 'label' => 'E-Mail',
  32. ))
  33. ->add('subject', TextType::class, array(
  34. 'label' => 'Betreff',
  35. 'attr' => array(
  36. 'pattern' => '.{3,}',
  37. 'label' => $subject
  38. )
  39. ))
  40. ->add('content', TextAreaType::class, array(
  41. 'label' => 'Nachricht',
  42. 'attr' => array(
  43. 'cols' => 50,
  44. 'rows' => 10)
  45. ))
  46. ->add('spam_content', TextType::class, array('label' => false ) )// bot protection!
  47. ->add('send', SubmitType::class, array('label' => 'Senden') )
  48. ;
  49. }
  50. public function setDefaultOptions(OptionsResolverInterface $resolver)
  51. {
  52. $collectionConstraint = new Collection(array(
  53. 'name' => array(
  54. new NotBlank(array('message' => 'Bitte ausfüllen.')),
  55. new Length(array('min' => 2))
  56. ),
  57. 'email' => array(
  58. new NotBlank(array('message' => 'Bitte ausfüllen.')),
  59. new Email(array('message' => 'Ungültige Adresse.'))
  60. ),
  61. 'subject' => array(
  62. new NotBlank(array('message' => 'Bitte ausfüllen.')),
  63. new Length(array('min' => 3))
  64. ),
  65. 'content' => array(
  66. ),
  67. 'spam_content' => array(
  68. new NotBlank(array('message' => 'Bitte ausfüllen.')),
  69. new Length(array('min' => 5))
  70. )
  71. ));
  72. $resolver->setDefaults(array(
  73. 'constraints' => $collectionConstraint
  74. ));
  75. }
  76. public function getName()
  77. {
  78. return 'contact';
  79. }
  80. }