Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation groups and constraints to gateway config fields #145

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/Form/Type/TpayGatewayConfigurationType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;

final class TpayGatewayConfigurationType extends AbstractType
{
Expand All @@ -35,13 +39,23 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
TextType::class,
[
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.client_id',
'constraints' => [
new NotBlank(groups: [
'commerce_weavers_sylius_tpay:shop:payment_method:default_type',
]),
],
],
)
->add(
'client_secret',
PasswordType::class,
[
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.client_secret',
// 'constraints' => [
// new NotBlank(groups: [
// 'commerce_weavers_sylius_tpay:shop:payment_method:default_type',
// ]),
// ],
],
)
->add(
Expand All @@ -51,6 +65,9 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.cards_api',
'required' => false,
'empty_data' => '',
'constraints' => [
new NotBlank(groups: ['commerce_weavers_sylius_tpay:shop:payment_method:card_type']),
],
],
)
->add(
Expand All @@ -74,22 +91,37 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
[
'empty_data' => '',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.merchant_id',
'constraints' => [
new NotBlank(groups: [
'commerce_weavers_sylius_tpay:shop:payment_method:default_type',
]),
],
],
)
->add(
'google_merchant_id',
TextType::class,
[
'empty_data' => '',
'required' => false,
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.google_merchant_id',
'constraints' => [
new NotBlank(groups: ['commerce_weavers_sylius_tpay:shop:payment_method:google_pay_type']),
],
],
)
->add(
'apple_pay_merchant_id',
TextType::class,
[
'empty_data' => '',
'required' => false,
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.apple_pay_merchant_id',
'constraints' => [
new NotBlank(groups: [
'commerce_weavers_sylius_tpay:shop:payment_method:apple_pay_type',
]),
],
],
)
->add(
Expand All @@ -98,6 +130,11 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
[
'empty_data' => '',
'label' => 'commerce_weavers_sylius_tpay.admin.gateway_configuration.notification_security_code',
'constraints' => [
new NotNull(groups: [
'commerce_weavers_sylius_tpay:shop:payment_method:default_type',
]),
],
],
)
->add(
Expand All @@ -117,4 +154,25 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
$builder->addEventListener(FormEvents::POST_SUBMIT, $this->encryptGatewayConfigListener);
$builder->addEventListener(FormEvents::PRE_SUBMIT, $this->preventSavingEmptyClientSecretListener);
}

public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(array(
'validation_groups' => function (FormInterface $form) {
/** @var array $data */
$data = $form->getData();

$defaultType = 'commerce_weavers_sylius_tpay:shop:payment_method:default_type';

$result = match($data['type']) {
PaymentType::CARD => 'commerce_weavers_sylius_tpay:shop:payment_method:card_type',
PaymentType::GOOGLE_PAY => 'commerce_weavers_sylius_tpay:shop:payment_method:google_pay_type',
PaymentType::APPLE_PAY => 'commerce_weavers_sylius_tpay:shop:payment_method:apple_pay_type',
default => $defaultType,
};

return $result === $defaultType ? [$result] : [$result, $defaultType];
},
));
}
}
Loading