diff --git a/Form/Type/FileUploadType.php b/Form/Type/FileUploadType.php index 10f18c326718df5051a8202e76a7d79797173659..1c8c0b41160409c65eea3246596eb55f067c7f2a 100644 --- a/Form/Type/FileUploadType.php +++ b/Form/Type/FileUploadType.php @@ -12,6 +12,8 @@ namespace Irstea\FileUploadBundle\Form\Type; use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer; use Irstea\FileUploadBundle\Model\FileManagerInterface; +use Irstea\FileUploadBundle\Validation\FileMimeType; +use Irstea\FileUploadBundle\Validation\FileSize; use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; @@ -22,6 +24,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Routing\Router; use Symfony\Component\Validator\Constraints\Count; +use Symfony\Component\Validator\Constraints\NotBlank; /** Type de champ 'file_upload'. * @@ -150,12 +153,19 @@ class FileUploadType extends AbstractType } } elseif ($options['required']) { - $required = new \Symfony\Component\Validator\Constraints\NotBlank(['message' => 'file_upload.required']); + $required = new NotBlank(['message' => 'file_upload.required']); $constraints[] = $required; } - if ($options['min_file_size'] || $options['max_file_size'] || $options['accept_file_types']) { - // @TODO gère les tailles + if ($options['min_file_size'] || $options['max_file_size']) { + $constraints[] = new FileSize([ + 'min' => $options['min_file_size'], + 'max' => $options['max_file_size'] + ]); + } + + if ($options['accept_file_types']) { + $constraints[] = new FileMimeType($options['accept_file_types']); } return $constraints; diff --git a/Validation/FileMimeType.php b/Validation/FileMimeType.php new file mode 100644 index 0000000000000000000000000000000000000000..a1d3aa0e0b05495390faf82c8f6107df5f45a216 --- /dev/null +++ b/Validation/FileMimeType.php @@ -0,0 +1,44 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileMimeType extends Constraint +{ + /** + * @var string + */ + public $acceptedMimeTypes = null; + + /** + * @var string + */ + public $message = 'file_upload.acceptFileTypes'; + + /** + * {@inheritdoc} + */ + public function getDefaultOption() + { + return 'acceptedMimeTypes'; + } + + /** + * {@inheritdoc} + */ + public function getRequiredOptions() + { + return ['acceptedMimeTypes']; + } +} diff --git a/Validation/FileMimeTypeValidator.php b/Validation/FileMimeTypeValidator.php new file mode 100644 index 0000000000000000000000000000000000000000..8e7f9a2ce06deaa7ee2b61f7832d4c8f3f821a39 --- /dev/null +++ b/Validation/FileMimeTypeValidator.php @@ -0,0 +1,37 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Irstea\FileUploadBundle\Model\UploadedFileInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * Vérifie que le type MIME du fichier valide la contrainte. + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileMimeTypeValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$value instanceof UploadedFileInterface) { + return; + } + if (preg_match('@^'.$constraint->acceptedMimeTypes.'$@i', $value->getMimeType())) { + return; + } + $this->context->buildViolation($constraint->message) + ->setParameter('%displayName%', $value->getDisplayName()) + ->setParameter('%mimeType%', $value->getMimeType()) + ->addViolation(); + } +} diff --git a/Validation/FileSize.php b/Validation/FileSize.php new file mode 100644 index 0000000000000000000000000000000000000000..976bae3195c7b41c7c5d0d91f24ed751d9d77689 --- /dev/null +++ b/Validation/FileSize.php @@ -0,0 +1,37 @@ +<?php +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Symfony\Component\Validator\Constraint; + +/** + * @Annotation + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileSize extends Constraint +{ + /** + * @var integer|null + */ + public $min = null; + + /** + * @var integer|null + */ + public $max = null; + + /** + * @var string + */ + public $minMessage = 'file_upload.minFileSize'; + + /** + * @var string + */ + public $maxMessage = 'file_upload.maxFileSize'; +} diff --git a/Validation/FileSizeValidator.php b/Validation/FileSizeValidator.php new file mode 100644 index 0000000000000000000000000000000000000000..fec88094490aae751acc1137e88c64f32af00e34 --- /dev/null +++ b/Validation/FileSizeValidator.php @@ -0,0 +1,42 @@ +<?php + +/* + * Copyright (C) 2015 IRSTEA + * All rights reserved. + */ + +namespace Irstea\FileUploadBundle\Validation; + +use Irstea\FileUploadBundle\Model\UploadedFileInterface; +use Symfony\Component\Validator\Constraint; +use Symfony\Component\Validator\ConstraintValidator; + +/** + * Vérifie que la taille du fichier + * + * @author Guillaume Perréal <guillaume.perreal@irstea.fr> + */ +class FileSizeValidator extends ConstraintValidator +{ + /** + * {@inheritdoc} + */ + public function validate($value, Constraint $constraint) + { + if (!$value instanceof UploadedFileInterface) { + return; + } + if ($constraint->min && $value->getSize() < $constraint->min) { + $this->context->buildViolation($constraint->minMessage) + ->setParameter('%displayName%', $value->getDisplayName()) + ->setParameter('%min%', $constraint->min) + ->addViolation(); + } + if ($constraint->max && $value->getSize() > $constraint->max) { + $this->context->buildViolation($constraint->maxMessage) + ->setParameter('%displayName%', $value->getDisplayName()) + ->setParameter('%max%', $constraint->max) + ->addViolation(); + } + } +}