Skip to content

Commit

Permalink
Change form validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrej-kuhnel committed Mar 27, 2020
1 parent 03d9c84 commit 7045084
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 37 deletions.
51 changes: 14 additions & 37 deletions src/Form/Extension/ShipmentZasilkovnaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,17 @@

use MangoSylius\SyliusZasilkovnaPlugin\Model\ZasilkovnaShippingMethodInterface;
use Sylius\Bundle\CoreBundle\Form\Type\Checkout\ShipmentType;
use Sylius\Component\Core\Model\ShipmentInterface;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Core\Repository\ShippingMethodRepositoryInterface;
use Sylius\Component\Shipping\Resolver\ShippingMethodsResolverInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Contracts\Translation\TranslatorInterface;

class ShipmentZasilkovnaExtension extends AbstractTypeExtension
{
Expand All @@ -31,12 +29,17 @@ class ShipmentZasilkovnaExtension extends AbstractTypeExtension
/** @var string[]; */
private $zasilkovnaMethodsCodes = [];

/** @var TranslatorInterface */
private $translator;

public function __construct(
ShippingMethodsResolverInterface $shippingMethodsResolver,
ShippingMethodRepositoryInterface $shippingMethodRepository
ShippingMethodRepositoryInterface $shippingMethodRepository,
TranslatorInterface $translator
) {
$this->shippingMethodsResolver = $shippingMethodsResolver;
$this->shippingMethodRepository = $shippingMethodRepository;
$this->translator = $translator;
}

/** @param array<mixed> $options */
Expand All @@ -60,6 +63,12 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
}

$event->setData($orderData);

// validation
$data = $event->getData();
if (array_key_exists('zasilkovna_' . $data['method'], $data) && !((bool) $orderData['zasilkovna_' . $orderData['method']])) {
$event->getForm()->addError(new FormError($this->translator->trans('mangoweb.shop.checkout.zasilkovnaBranch', [], 'validators')));
}
})
->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
$form = $event->getForm();
Expand Down Expand Up @@ -102,12 +111,6 @@ public function buildForm(FormBuilderInterface $builder, array $options): void
'required' => false,
'mapped' => false,
'empty_data' => null,
'constraints' => [
new NotBlank([
'groups' => ['zasilkovna_' . $method->getCode()],
'message' => 'mangoweb.shop.checkout.zasilkovnaBranch',
]),
],
]);
}
}
Expand Down Expand Up @@ -147,32 +150,6 @@ private function getZasilkovnaName(array $zasilkovna): string
return implode(', ', $arrayName);
}

public function configureOptions(OptionsResolver $resolver): void
{
parent::configureOptions($resolver);

$validationGroups = $resolver->resolve()['validation_groups'];

$resolver->setDefaults([
'validation_groups' => function (FormInterface $form) use ($validationGroups) {
$entity = $form->getData();
assert($entity instanceof ShipmentInterface);

$shippingMethod = $entity->getMethod();

if ($shippingMethod !== null) {
assert($shippingMethod instanceof ZasilkovnaShippingMethodInterface);
$zasilkovnaConfig = $shippingMethod->getZasilkovnaConfig();
if ($zasilkovnaConfig && $zasilkovnaConfig->getApiKey()) {
$validationGroups = array_merge($validationGroups ?? [], ['zasilkovna_' . $shippingMethod->getCode()]);
}
}

return $validationGroups;
},
]);
}

/** @return array<string> */
public static function getExtendedTypes(): array
{
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
arguments:
- '@sylius.shipping_methods_resolver'
- '@sylius.repository.shipping_method'
- '@translator'
tags:
- name: form.type_extension
extended_type: Sylius\Bundle\CoreBundle\Form\Type\Checkout\ShipmentType
Expand Down

0 comments on commit 7045084

Please sign in to comment.