Skip to content

Commit 6dfc6d9

Browse files
author
Maxime Leclercq
committed
feat: add message on constraint and prefill name with customer fullname
1 parent 290a37e commit 6dfc6d9

File tree

3 files changed

+47
-12
lines changed

3 files changed

+47
-12
lines changed

src/Form/Extension/ContactTypeExtension.php

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,21 @@
1515

1616
use MonsieurBiz\SyliusSettingsPlugin\Provider\SettingsProviderInterface;
1717
use Sylius\Bundle\CoreBundle\Form\Type\ContactType;
18+
use Sylius\Component\Customer\Context\CustomerContextInterface;
1819
use Symfony\Component\Form\AbstractTypeExtension;
1920
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
2021
use Symfony\Component\Form\Extension\Core\Type\TelType;
2122
use Symfony\Component\Form\Extension\Core\Type\TextType;
2223
use Symfony\Component\Form\FormBuilderInterface;
24+
use Symfony\Component\Form\FormEvent;
25+
use Symfony\Component\Form\FormEvents;
2326
use Symfony\Component\Validator\Constraints as Assert;
2427

2528
final class ContactTypeExtension extends AbstractTypeExtension
2629
{
2730
public function __construct(
2831
private SettingsProviderInterface $settingProvider,
32+
private CustomerContextInterface $customerContext,
2933
) {
3034
}
3135

@@ -36,15 +40,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
3640
{
3741
parent::buildForm($builder, $options);
3842

39-
$defaultConstraints = [
40-
new Assert\Length(['max' => 255]),
41-
];
42-
43-
$requiredConstraints = [
44-
new Assert\NotBlank(),
45-
new Assert\Length(['max' => 255]),
46-
];
47-
4843
$isNameRequired = $this->isFieldRequired('field_name_required', 'field_name_displayed');
4944
$isCompanyRequired = $this->isFieldRequired('field_company_required', 'field_company_displayed');
5045
$isPhoneNumberRequired = $this->isFieldRequired('field_phone_number_required', 'field_phone_number_displayed');
@@ -53,17 +48,18 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
5348
->add('name', TextType::class, [
5449
'label' => 'monsieurbiz.contact_request.form.name',
5550
'required' => $isNameRequired,
56-
'constraints' => $isNameRequired ? $requiredConstraints : $defaultConstraints,
51+
'constraints' => $this->getConstraints($isNameRequired, 'monsieurbiz.contact_request.name.not_blank'),
5752
])
5853
->add('company', TextType::class, [
5954
'label' => 'monsieurbiz.contact_request.form.company',
6055
'required' => $isCompanyRequired,
61-
'constraints' => $isCompanyRequired ? $requiredConstraints : $defaultConstraints,
56+
'constraints' => $this->getConstraints($isCompanyRequired, 'monsieurbiz.contact_request.company.not_blank'),
6257
])
6358
->add('phoneNumber', TelType::class, [
6459
'label' => 'monsieurbiz.contact_request.form.phone_number',
60+
'invalid_message' => 'monsieurbiz.contact_request.phone_number.invalid',
6561
'required' => $isPhoneNumberRequired,
66-
'constraints' => $isPhoneNumberRequired ? $requiredConstraints : $defaultConstraints,
62+
'constraints' => $this->getConstraints($isPhoneNumberRequired, 'monsieurbiz.contact_request.phone_number.not_blank'),
6763
])
6864
;
6965

@@ -82,6 +78,19 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
8278
],
8379
]);
8480
}
81+
82+
$builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
83+
$name = $this->customerContext->getCustomer()?->getFullName();
84+
if (null === $name) {
85+
return;
86+
}
87+
88+
/** @var array $data */
89+
$data = $event->getData();
90+
$data['name'] = $name;
91+
92+
$event->setData($data);
93+
});
8594
}
8695

8796
public static function getExtendedTypes(): iterable
@@ -96,4 +105,16 @@ private function isFieldRequired(string $path, string $pathDisplayed): bool
96105
return $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $path)
97106
&& $this->settingProvider->getSettingValue('monsieurbiz_contact_request.contact', $pathDisplayed);
98107
}
108+
109+
private function getConstraints(bool $isRequired, ?string $blankMessage): array
110+
{
111+
return $isRequired ? [
112+
new Assert\NotBlank([
113+
'message' => $blankMessage,
114+
]),
115+
new Assert\Length(['max' => 255]),
116+
] : [
117+
new Assert\Length(['max' => 255]),
118+
];
119+
}
99120
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
monsieurbiz:
22
contact_request:
33
confirmation_error: 'You need to approve this field'
4+
name:
5+
not_blank: Please enter your name
6+
company:
7+
not_blank: Please enter your company name
8+
phone_number:
9+
invalid: Please enter a valid phone number
10+
not_blank: Please enter your phone number
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
monsieurbiz:
22
contact_request:
33
confirmation_error: 'Vous devez approuver ce champ'
4+
name:
5+
not_blank: Veuillez entrer votre nom
6+
company:
7+
not_blank: Veuillez entrer le nom de votre société
8+
phone_number:
9+
invalid: Veuillez entrer un numéro de téléphone valide
10+
not_blank: Veuillez entrer votre numéro de téléphone

0 commit comments

Comments
 (0)