src/Form/Abo/AboBestellungType.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Form\Abo;
  3. use App\Entity\Abo\AboBestellung;
  4. use Symfony\Component\Form\AbstractType;
  5. use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
  6. use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
  7. use Symfony\Component\Form\Extension\Core\Type\EmailType;
  8. use Symfony\Component\Form\Extension\Core\Type\TextareaType;
  9. use Symfony\Component\Form\Extension\Core\Type\TextType;
  10. use Symfony\Component\Form\FormBuilderInterface;
  11. use Symfony\Component\OptionsResolver\OptionsResolver;
  12. use Symfony\Component\Routing\RouterInterface;
  13. use Symfony\Component\Validator\Constraints\Callback;
  14. use Symfony\Component\Validator\Constraints\Email;
  15. use Symfony\Component\Validator\Constraints\Length;
  16. use Symfony\Component\Validator\Constraints\NotBlank;
  17. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  18. class AboBestellungType extends AbstractType
  19. {
  20. public function __construct(private readonly RouterInterface $router)
  21. {
  22. }
  23. #[\Override]
  24. public function buildForm(FormBuilderInterface $builder, array $options): void
  25. {
  26. if ($options['data']->formAboOptions()) {
  27. $builder->add('aboOption', ChoiceType::class,
  28. [
  29. 'label' => 'Angebote',
  30. 'required' => true,
  31. 'choices' => $options['data']->formAboOptions(),
  32. 'expanded' => true,
  33. 'multiple' => false,
  34. ]
  35. );
  36. }
  37. if ($options['data']->useEpaperLoginFields()) {
  38. $builder->add('emailEpaperLogin', EmailType::class,
  39. [
  40. 'label' => 'E-Mail',
  41. 'required' => true,
  42. 'constraints' => [
  43. new NotBlank(),
  44. new Length(['max' => 100]),
  45. ],
  46. ]
  47. );
  48. }
  49. $builder
  50. ->add('vorname', TextType::class,
  51. [
  52. 'required' => true,
  53. 'attr' => ['maxlength' => 100],
  54. 'constraints' => [
  55. new NotBlank(),
  56. new Length(['max' => 100]),
  57. ],
  58. ]
  59. )
  60. ->add('nachname', TextType::class,
  61. [
  62. 'required' => true,
  63. 'label' => 'Name',
  64. 'attr' => ['maxlength' => 100],
  65. 'constraints' => [
  66. new NotBlank(),
  67. new Length(['max' => 100]),
  68. ],
  69. ]
  70. )
  71. ->add('firma', TextType::class,
  72. [
  73. 'required' => false,
  74. 'attr' => ['maxlength' => 100],
  75. 'constraints' => [
  76. new Length(['max' => 100]),
  77. ],
  78. ]
  79. )
  80. ->add('strasse', TextType::class,
  81. [
  82. 'required' => true,
  83. 'attr' => ['maxlength' => 100],
  84. 'constraints' => [
  85. new NotBlank(),
  86. new Length(['max' => 100]),
  87. ],
  88. 'label' => 'Strasse/Nr.',
  89. ]
  90. )
  91. ->add('plz', TextType::class,
  92. [
  93. 'label' => 'PLZ',
  94. 'required' => true,
  95. 'attr' => ['maxlength' => 100],
  96. 'constraints' => [
  97. new NotBlank(),
  98. new Length(['max' => 100]),
  99. ],
  100. ]
  101. )
  102. ->add('ort', TextType::class,
  103. [
  104. 'required' => true,
  105. 'attr' => ['maxlength' => 100],
  106. 'constraints' => [
  107. new NotBlank(),
  108. new Length(['max' => 100]),
  109. ],
  110. ]
  111. )
  112. ->add('land', TextType::class,
  113. [
  114. 'required' => true,
  115. 'attr' => ['maxlength' => 100],
  116. 'constraints' => [
  117. new NotBlank(),
  118. new Length(['max' => 100]),
  119. ],
  120. ]
  121. )
  122. ->add('telefon', TextType::class,
  123. [
  124. 'required' => true,
  125. 'attr' => ['maxlength' => 100],
  126. 'constraints' => [
  127. new NotBlank(),
  128. new Length(['max' => 100]),
  129. ],
  130. ]
  131. )
  132. ->add('email', EmailType::class,
  133. [
  134. 'required' => true,
  135. 'label' => 'E-Mail',
  136. 'attr' => ['maxlength' => 100],
  137. 'constraints' => [
  138. new NotBlank(),
  139. new Email(),
  140. new Length(['max' => 100]),
  141. ],
  142. ]
  143. )
  144. ->add('mitteilung', TextareaType::class,
  145. [
  146. 'required' => false,
  147. 'attr' => ['maxlength' => 5000],
  148. 'constraints' => [
  149. new Length(['max' => 5000]),
  150. ],
  151. ]
  152. )
  153. ->add('useRechnungsKontakt', CheckboxType::class,
  154. [
  155. 'required' => false,
  156. 'label' => 'Abweichende Lieferadresse',
  157. ]
  158. )
  159. ->add('rechnungVorname', TextType::class,
  160. [
  161. 'required' => true,
  162. 'label' => 'Vorname',
  163. 'attr' => ['maxlength' => 100],
  164. 'constraints' => [
  165. new Length(['max' => 100]),
  166. ],
  167. ]
  168. )
  169. ->add('rechnungNachname', TextType::class,
  170. [
  171. 'required' => true,
  172. 'label' => 'Name',
  173. 'attr' => ['maxlength' => 100],
  174. 'constraints' => [
  175. new Length(['max' => 100]),
  176. ],
  177. ]
  178. )
  179. ->add('rechnungFirma', TextType::class,
  180. [
  181. 'required' => false,
  182. 'label' => 'Firma',
  183. 'attr' => ['maxlength' => 100],
  184. 'constraints' => [
  185. new Length(['max' => 100]),
  186. ],
  187. ]
  188. )
  189. ->add('rechnungStrasse', TextType::class,
  190. [
  191. 'required' => true,
  192. 'label' => 'Strasse',
  193. 'attr' => ['maxlength' => 100],
  194. 'constraints' => [
  195. new Length(['max' => 100]),
  196. ],
  197. 'label' => 'Strasse/Nr.',
  198. ]
  199. )
  200. ->add('rechnungPlz', TextType::class,
  201. [
  202. 'required' => true,
  203. 'label' => 'PLZ',
  204. 'attr' => ['maxlength' => 100],
  205. 'constraints' => [
  206. new Length(['max' => 100]),
  207. ],
  208. ]
  209. )
  210. ->add('rechnungOrt', TextType::class,
  211. [
  212. 'required' => true,
  213. 'label' => 'Ort',
  214. 'attr' => ['maxlength' => 100],
  215. 'constraints' => [
  216. new Length(['max' => 100]),
  217. ],
  218. ]
  219. )
  220. ->add('rechnungLand', TextType::class,
  221. [
  222. 'required' => true,
  223. 'label' => 'Land',
  224. 'attr' => ['maxlength' => 100],
  225. 'constraints' => [
  226. new Length(['max' => 100]),
  227. ],
  228. ]
  229. )
  230. ->add('datenschutz', CheckboxType::class,
  231. [
  232. 'mapped' => false,
  233. 'required' => true,
  234. 'label' => 'Ich habe die <a href="'.$this->router->generate('fe.page_datenschutz').'" target="_blank">Datenschutzrichtlinien</a> gelesen und bin damit einverstanden.',
  235. 'label_html' => true,
  236. 'constraints' => [
  237. new NotBlank(),
  238. ],
  239. ]
  240. )
  241. ;
  242. }
  243. #[\Override]
  244. public function configureOptions(OptionsResolver $resolver): void
  245. {
  246. $resolver->setDefaults(
  247. [
  248. 'constraints' => [
  249. new Callback([
  250. 'callback' => $this->checkRechnungKontakt(...),
  251. ]),
  252. ],
  253. ]
  254. );
  255. }
  256. public function checkRechnungKontakt($value, ExecutionContextInterface $context): void
  257. {
  258. $mustMsg = 'Dieses Feld muss belegt werden';
  259. /**
  260. * @var AboBestellung $value
  261. */
  262. if ($value->useRechnungsKontakt) {
  263. if (!$value->rechnungVorname) {
  264. $context
  265. ->buildViolation($mustMsg)
  266. ->atPath('rechnungVorname')
  267. ->addViolation()
  268. ;
  269. }
  270. if (!$value->rechnungNachname) {
  271. $context
  272. ->buildViolation($mustMsg)
  273. ->atPath('rechnungNachname')
  274. ->addViolation()
  275. ;
  276. }
  277. if (!$value->rechnungStrasse) {
  278. $context
  279. ->buildViolation($mustMsg)
  280. ->atPath('rechnungStrasse')
  281. ->addViolation()
  282. ;
  283. }
  284. if (!$value->rechnungPlz) {
  285. $context
  286. ->buildViolation($mustMsg)
  287. ->atPath('rechnungPlz')
  288. ->addViolation()
  289. ;
  290. }
  291. if (!$value->rechnungOrt) {
  292. $context
  293. ->buildViolation($mustMsg)
  294. ->atPath('rechnungOrt')
  295. ->addViolation()
  296. ;
  297. }
  298. if (!$value->rechnungLand) {
  299. $context
  300. ->buildViolation($mustMsg)
  301. ->atPath('rechnungLand')
  302. ->addViolation()
  303. ;
  304. }
  305. }
  306. }
  307. }