From bcce61b98321fe78b055ee509693a8401fef61e6 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Thu, 6 Aug 2020 17:12:10 +0200 Subject: [PATCH 1/5] Handle the empty background image Also make it required Fixes #66 --- src/Form/Type/GiftCardConfigurationType.php | 16 ++++++++++ src/Resources/config/services/validator.xml | 11 +++++++ .../validation/GiftCardConfiguration.xml | 7 +++++ src/Resources/translations/validators.en.yml | 3 ++ .../GiftCardConfiguration/_form.html.twig | 1 + .../views/Shop/GiftCard/pdf.html.twig | 4 +-- .../Constraints/HasBackgroundImage.php | 18 +++++++++++ .../HasBackgroundImageValidator.php | 31 +++++++++++++++++++ 8 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 src/Resources/config/services/validator.xml create mode 100644 src/Validator/Constraints/HasBackgroundImage.php create mode 100644 src/Validator/Constraints/HasBackgroundImageValidator.php diff --git a/src/Form/Type/GiftCardConfigurationType.php b/src/Form/Type/GiftCardConfigurationType.php index 26988f7f..f845f87c 100644 --- a/src/Form/Type/GiftCardConfigurationType.php +++ b/src/Form/Type/GiftCardConfigurationType.php @@ -9,6 +9,8 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\Form\FormInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; final class GiftCardConfigurationType extends AbstractResourceType { @@ -43,4 +45,18 @@ public function getBlockPrefix(): string { return 'setono_sylius_gift_card_gift_card_configuration'; } + + public function configureOptions(OptionsResolver $resolver): void + { + parent::configureOptions($resolver); + $resolver->setDefault('validation_groups', function (FormInterface $form): array { + $validationGroups = $this->validationGroups; + $data = $form->getData(); + if (null === $data->getId()) { + $validationGroups[] = 'setono_sylius_gift_card_create'; + } + + return $validationGroups; + }); + } } diff --git a/src/Resources/config/services/validator.xml b/src/Resources/config/services/validator.xml new file mode 100644 index 00000000..77ecf0f5 --- /dev/null +++ b/src/Resources/config/services/validator.xml @@ -0,0 +1,11 @@ + + + + + + + + + diff --git a/src/Resources/config/validation/GiftCardConfiguration.xml b/src/Resources/config/validation/GiftCardConfiguration.xml index 77fbc4f4..b71c8e61 100644 --- a/src/Resources/config/validation/GiftCardConfiguration.xml +++ b/src/Resources/config/validation/GiftCardConfiguration.xml @@ -9,6 +9,13 @@ + + + + diff --git a/src/Resources/translations/validators.en.yml b/src/Resources/translations/validators.en.yml index 4b3abbe2..5f37617f 100644 --- a/src/Resources/translations/validators.en.yml +++ b/src/Resources/translations/validators.en.yml @@ -15,6 +15,9 @@ setono_sylius_gift_card: integer: Amount must be an integer min: Amount cannot be lower than 1 greater_than_or_equal: The amount must be greater than or equal to 1 + gift_card_configuration: + create: + background_image_required: The background image is required gift_card_search_command: gift_card: does_not_exist: The gift card with this code does not exist diff --git a/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig b/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig index a6d63dd3..a653daf4 100644 --- a/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig +++ b/src/Resources/views/Admin/GiftCardConfiguration/_form.html.twig @@ -1,6 +1,7 @@ {% form_theme form '@SyliusAdmin/Form/imagesTheme.html.twig' %}
+ {{ form_errors(form) }}
{{ form_row(form.code) }} diff --git a/src/Resources/views/Shop/GiftCard/pdf.html.twig b/src/Resources/views/Shop/GiftCard/pdf.html.twig index 7481adb3..554e0b4c 100644 --- a/src/Resources/views/Shop/GiftCard/pdf.html.twig +++ b/src/Resources/views/Shop/GiftCard/pdf.html.twig @@ -5,10 +5,8 @@ {% if configuration.backgroundImage is not empty %} {% set path = configuration.backgroundImage.path|imagine_filter('setono_sylius_gift_card_background') %} -{% else %} - {% set path = '//placehold.it/200x200' %} + {% endif %} - {{- giftCard.code -}} diff --git a/src/Validator/Constraints/HasBackgroundImage.php b/src/Validator/Constraints/HasBackgroundImage.php new file mode 100644 index 00000000..67d43ea0 --- /dev/null +++ b/src/Validator/Constraints/HasBackgroundImage.php @@ -0,0 +1,18 @@ +getBackgroundImage(); + if (!$backgroundImage instanceof GiftCardConfigurationImageInterface || null === $backgroundImage->getPath()) { + $this->context->buildViolation($constraint->message) + ->addViolation(); + } + } +} From b21510abd01ec5ff6523024399a3a9520cddca2c Mon Sep 17 00:00:00 2001 From: Roshyo Date: Thu, 6 Aug 2020 17:13:27 +0200 Subject: [PATCH 2/5] Adding french translation --- src/Resources/translations/validators.fr.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Resources/translations/validators.fr.yml b/src/Resources/translations/validators.fr.yml index 8e1f14ec..fdf8f969 100644 --- a/src/Resources/translations/validators.fr.yml +++ b/src/Resources/translations/validators.fr.yml @@ -14,6 +14,9 @@ setono_sylius_gift_card: not_blank: Veuillez renseigner un montant integer: Le montant doit être un entier min: Le montant ne dois pas être en-dessous de 1 + gift_card_configuration: + create: + background_image_required: L'image de fond est requise gift_card_search_command: gift_card: does_not_exist: Ce code de chèque-cadeau n'existe pas From a03bae4b0f65825e50d76a7fc6c2ec46b37a9d98 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Thu, 6 Aug 2020 21:06:42 +0200 Subject: [PATCH 3/5] Removing useless form configuration --- src/Form/Type/GiftCardConfigurationType.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/src/Form/Type/GiftCardConfigurationType.php b/src/Form/Type/GiftCardConfigurationType.php index f845f87c..26988f7f 100644 --- a/src/Form/Type/GiftCardConfigurationType.php +++ b/src/Form/Type/GiftCardConfigurationType.php @@ -9,8 +9,6 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\Form\FormInterface; -use Symfony\Component\OptionsResolver\OptionsResolver; final class GiftCardConfigurationType extends AbstractResourceType { @@ -45,18 +43,4 @@ public function getBlockPrefix(): string { return 'setono_sylius_gift_card_gift_card_configuration'; } - - public function configureOptions(OptionsResolver $resolver): void - { - parent::configureOptions($resolver); - $resolver->setDefault('validation_groups', function (FormInterface $form): array { - $validationGroups = $this->validationGroups; - $data = $form->getData(); - if (null === $data->getId()) { - $validationGroups[] = 'setono_sylius_gift_card_create'; - } - - return $validationGroups; - }); - } } From 5d029b90bb7c9c5d2047b17aa91809e5d0a83b18 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Fri, 7 Aug 2020 13:15:02 +0200 Subject: [PATCH 4/5] Including validator file in services Removing useless level in translation Reworking condition --- src/Resources/config/services.xml | 1 + src/Resources/config/validation/GiftCardConfiguration.xml | 1 - src/Resources/translations/validators.en.yml | 3 +-- src/Resources/translations/validators.fr.yml | 3 +-- src/Validator/Constraints/HasBackgroundImage.php | 2 +- src/Validator/Constraints/HasBackgroundImageValidator.php | 2 +- 6 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 2159435c..f4043b43 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -15,6 +15,7 @@ + diff --git a/src/Resources/config/validation/GiftCardConfiguration.xml b/src/Resources/config/validation/GiftCardConfiguration.xml index b71c8e61..da19615e 100644 --- a/src/Resources/config/validation/GiftCardConfiguration.xml +++ b/src/Resources/config/validation/GiftCardConfiguration.xml @@ -12,7 +12,6 @@ diff --git a/src/Resources/translations/validators.en.yml b/src/Resources/translations/validators.en.yml index 5f37617f..c071b929 100644 --- a/src/Resources/translations/validators.en.yml +++ b/src/Resources/translations/validators.en.yml @@ -16,8 +16,7 @@ setono_sylius_gift_card: min: Amount cannot be lower than 1 greater_than_or_equal: The amount must be greater than or equal to 1 gift_card_configuration: - create: - background_image_required: The background image is required + background_image_required: The background image is required gift_card_search_command: gift_card: does_not_exist: The gift card with this code does not exist diff --git a/src/Resources/translations/validators.fr.yml b/src/Resources/translations/validators.fr.yml index fdf8f969..7cffe2c9 100644 --- a/src/Resources/translations/validators.fr.yml +++ b/src/Resources/translations/validators.fr.yml @@ -15,8 +15,7 @@ setono_sylius_gift_card: integer: Le montant doit être un entier min: Le montant ne dois pas être en-dessous de 1 gift_card_configuration: - create: - background_image_required: L'image de fond est requise + background_image_required: L'image de fond est requise gift_card_search_command: gift_card: does_not_exist: Ce code de chèque-cadeau n'existe pas diff --git a/src/Validator/Constraints/HasBackgroundImage.php b/src/Validator/Constraints/HasBackgroundImage.php index 67d43ea0..acb71db1 100644 --- a/src/Validator/Constraints/HasBackgroundImage.php +++ b/src/Validator/Constraints/HasBackgroundImage.php @@ -9,7 +9,7 @@ final class HasBackgroundImage extends Constraint { /** @var string */ - public $message = 'setono_sylius_gift_card.gift_card_configuration.create.background_image_required'; + public $message = 'setono_sylius_gift_card.gift_card_configuration.background_image_required'; public function getTargets(): array { diff --git a/src/Validator/Constraints/HasBackgroundImageValidator.php b/src/Validator/Constraints/HasBackgroundImageValidator.php index 636f71ce..367427ad 100644 --- a/src/Validator/Constraints/HasBackgroundImageValidator.php +++ b/src/Validator/Constraints/HasBackgroundImageValidator.php @@ -23,7 +23,7 @@ public function validate($value, Constraint $constraint): void } $backgroundImage = $value->getBackgroundImage(); - if (!$backgroundImage instanceof GiftCardConfigurationImageInterface || null === $backgroundImage->getPath()) { + if (null === $backgroundImage || null === $backgroundImage->getPath()) { $this->context->buildViolation($constraint->message) ->addViolation(); } From 789a53cda867b94159fa2a74188e0ada3518a184 Mon Sep 17 00:00:00 2001 From: Roshyo Date: Fri, 7 Aug 2020 13:22:15 +0200 Subject: [PATCH 5/5] Fix tests and checks --- src/Resources/config/services/validator.xml | 2 +- src/Validator/Constraints/HasBackgroundImageValidator.php | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Resources/config/services/validator.xml b/src/Resources/config/services/validator.xml index 77ecf0f5..39917049 100644 --- a/src/Resources/config/services/validator.xml +++ b/src/Resources/config/services/validator.xml @@ -4,7 +4,7 @@ xmlns="http://symfony.com/schema/dic/services" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> - + diff --git a/src/Validator/Constraints/HasBackgroundImageValidator.php b/src/Validator/Constraints/HasBackgroundImageValidator.php index 367427ad..ef14c387 100644 --- a/src/Validator/Constraints/HasBackgroundImageValidator.php +++ b/src/Validator/Constraints/HasBackgroundImageValidator.php @@ -4,7 +4,6 @@ namespace Setono\SyliusGiftCardPlugin\Validator\Constraints; -use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationImageInterface; use Setono\SyliusGiftCardPlugin\Model\GiftCardConfigurationInterface; use Symfony\Component\Validator\Constraint; use Symfony\Component\Validator\ConstraintValidator;