src/App/Form/Type/EventListener/FormatRutListener.php line 18

Open in your IDE?
  1. <?php
  2. namespace App\Form\Type\EventListener;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\Form\FormEvent;
  5. use Symfony\Component\Form\FormEvents;
  6. class FormatRutListener implements EventSubscriberInterface
  7. {
  8.     public static function getSubscribedEvents()
  9.     {
  10.         return [
  11.             FormEvents::PRE_SUBMIT => 'onPreSubmit',
  12.         ];
  13.     }
  14.     public function onPreSubmit(FormEvent $event)
  15.     {
  16.         $order $event->getData();
  17.         $form $event->getForm();
  18.         if (!$order) {
  19.             return;
  20.         }
  21.         $customer $order['customer'];
  22.         if ($customer) {
  23.             $rut $this->formatRut($customer['rut']);
  24.             
  25.             $customer['rut'] = $rut;
  26.             $order['customer'] = $customer;
  27.             
  28.             $event->setData($order);
  29.         }
  30.     }
  31.     private function formatRut(?string $rut): string
  32.     {
  33.         return $rut str_replace('.'''$rut) : '';
  34.     }
  35. }