<?php
namespace App\Form\Abo;
use App\Entity\Abo\AboBestellung;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
class AboBestellungType extends AbstractType
{
public function __construct(private readonly RouterInterface $router)
{
}
#[\Override]
public function buildForm(FormBuilderInterface $builder, array $options): void
{
if ($options['data']->formAboOptions()) {
$builder->add('aboOption', ChoiceType::class,
[
'label' => 'Angebote',
'required' => true,
'choices' => $options['data']->formAboOptions(),
'expanded' => true,
'multiple' => false,
]
);
}
if ($options['data']->useEpaperLoginFields()) {
$builder->add('emailEpaperLogin', EmailType::class,
[
'label' => 'E-Mail',
'required' => true,
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
);
}
$builder
->add('vorname', TextType::class,
[
'required' => true,
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
)
->add('nachname', TextType::class,
[
'required' => true,
'label' => 'Name',
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
)
->add('firma', TextType::class,
[
'required' => false,
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('strasse', TextType::class,
[
'required' => true,
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
'label' => 'Strasse/Nr.',
]
)
->add('plz', TextType::class,
[
'label' => 'PLZ',
'required' => true,
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
)
->add('ort', TextType::class,
[
'required' => true,
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
)
->add('land', TextType::class,
[
'required' => true,
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
)
->add('telefon', TextType::class,
[
'required' => true,
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Length(['max' => 100]),
],
]
)
->add('email', EmailType::class,
[
'required' => true,
'label' => 'E-Mail',
'attr' => ['maxlength' => 100],
'constraints' => [
new NotBlank(),
new Email(),
new Length(['max' => 100]),
],
]
)
->add('mitteilung', TextareaType::class,
[
'required' => false,
'attr' => ['maxlength' => 5000],
'constraints' => [
new Length(['max' => 5000]),
],
]
)
->add('useRechnungsKontakt', CheckboxType::class,
[
'required' => false,
'label' => 'Abweichende Lieferadresse',
]
)
->add('rechnungVorname', TextType::class,
[
'required' => true,
'label' => 'Vorname',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('rechnungNachname', TextType::class,
[
'required' => true,
'label' => 'Name',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('rechnungFirma', TextType::class,
[
'required' => false,
'label' => 'Firma',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('rechnungStrasse', TextType::class,
[
'required' => true,
'label' => 'Strasse',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
'label' => 'Strasse/Nr.',
]
)
->add('rechnungPlz', TextType::class,
[
'required' => true,
'label' => 'PLZ',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('rechnungOrt', TextType::class,
[
'required' => true,
'label' => 'Ort',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('rechnungLand', TextType::class,
[
'required' => true,
'label' => 'Land',
'attr' => ['maxlength' => 100],
'constraints' => [
new Length(['max' => 100]),
],
]
)
->add('datenschutz', CheckboxType::class,
[
'mapped' => false,
'required' => true,
'label' => 'Ich habe die <a href="'.$this->router->generate('fe.page_datenschutz').'" target="_blank">Datenschutzrichtlinien</a> gelesen und bin damit einverstanden.',
'label_html' => true,
'constraints' => [
new NotBlank(),
],
]
)
;
}
#[\Override]
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(
[
'constraints' => [
new Callback([
'callback' => $this->checkRechnungKontakt(...),
]),
],
]
);
}
public function checkRechnungKontakt($value, ExecutionContextInterface $context): void
{
$mustMsg = 'Dieses Feld muss belegt werden';
/**
* @var AboBestellung $value
*/
if ($value->useRechnungsKontakt) {
if (!$value->rechnungVorname) {
$context
->buildViolation($mustMsg)
->atPath('rechnungVorname')
->addViolation()
;
}
if (!$value->rechnungNachname) {
$context
->buildViolation($mustMsg)
->atPath('rechnungNachname')
->addViolation()
;
}
if (!$value->rechnungStrasse) {
$context
->buildViolation($mustMsg)
->atPath('rechnungStrasse')
->addViolation()
;
}
if (!$value->rechnungPlz) {
$context
->buildViolation($mustMsg)
->atPath('rechnungPlz')
->addViolation()
;
}
if (!$value->rechnungOrt) {
$context
->buildViolation($mustMsg)
->atPath('rechnungOrt')
->addViolation()
;
}
if (!$value->rechnungLand) {
$context
->buildViolation($mustMsg)
->atPath('rechnungLand')
->addViolation()
;
}
}
}
}