<?php
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
*/
namespace App\Validator;
use App\Entity\DocumentUpload;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\File\UploadedFile;
/**
* Description of DocumentUploadHeaderValidator
*
* @author programmer
*/
class ExtensionValidator extends AbstractExcelValidator
{
public function getPriority(): int
{
return 100;
}
public function validate(FormEvent $evt):void
{
$entity = $evt->getData();
if (!$entity instanceof DocumentUpload) {
return;
}
if (null === $entity->getDocument()) {
$evt->getForm()
->get("document")
->addError(
new FormError("harus diisi.")
);
return;
}
if (null === $entity->getAttachment()) {
$evt->getForm()
->get("attachment")
->addError(
new FormError("harus diisi.")
);
return;
}
$allows = ['xls','xlsx'];
$file = new UploadedFile($entity->getAttachment(), basename($entity->getAttachment()));
if (!in_array($file->getExtension(), $allows)) {
$evt->getForm()
->get("attachment")
->addError(
new FormError(sprintf("invalid type. allowed %s", implode(", ",$allows)))
);
return;
}
}
}