src/Validator/ExtensionValidator.php line 27

Open in your IDE?
  1. <?php
  2. /*
  3.  * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
  4.  * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
  5.  */
  6. namespace App\Validator;
  7. use App\Entity\DocumentUpload;
  8. use Symfony\Component\Form\FormEvent;
  9. use Symfony\Component\Form\FormError;
  10. use Symfony\Component\HttpFoundation\File\UploadedFile;
  11. /**
  12.  * Description of DocumentUploadHeaderValidator
  13.  *
  14.  * @author programmer
  15.  */
  16. class ExtensionValidator extends AbstractExcelValidator 
  17. {
  18.     public function getPriority(): int 
  19.     {
  20.         return 100;
  21.     }
  22.     
  23.     public function validate(FormEvent $evt):void
  24.     {
  25.         $entity $evt->getData();
  26.         if (!$entity instanceof DocumentUpload) {
  27.             return;
  28.         }
  29.         
  30.         if (null === $entity->getDocument()) {
  31.             $evt->getForm()
  32.                     ->get("document")
  33.                     ->addError(
  34.                         new FormError("harus diisi.")
  35.                     );
  36.             return;
  37.         }
  38.         
  39.         if (null === $entity->getAttachment()) {
  40.             $evt->getForm()
  41.                     ->get("attachment")
  42.                     ->addError(
  43.                         new FormError("harus diisi.")
  44.                     );
  45.             return;
  46.         }
  47.         
  48.         $allows = ['xls','xlsx'];
  49.         $file = new UploadedFile($entity->getAttachment(), basename($entity->getAttachment()));
  50.         if (!in_array($file->getExtension(), $allows)) {
  51.             $evt->getForm()
  52.                     ->get("attachment")
  53.                     ->addError(
  54.                         new FormError(sprintf("invalid type. allowed %s"implode(", ",$allows)))
  55.                     );
  56.             return;
  57.         }
  58.     }
  59. }