Commit fbf95c50 authored by Guillaume Perréal's avatar Guillaume Perréal
Browse files

Ajout de la validation du type et de la taille côté serveur.

Showing with 173 additions and 3 deletions
+173 -3
...@@ -12,6 +12,8 @@ namespace Irstea\FileUploadBundle\Form\Type; ...@@ -12,6 +12,8 @@ namespace Irstea\FileUploadBundle\Form\Type;
use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer; use Irstea\FileUploadBundle\Form\DataTranformer\UploadedFileTransformer;
use Irstea\FileUploadBundle\Model\FileManagerInterface; use Irstea\FileUploadBundle\Model\FileManagerInterface;
use Irstea\FileUploadBundle\Validation\FileMimeType;
use Irstea\FileUploadBundle\Validation\FileSize;
use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer; use Symfony\Bridge\Doctrine\Form\DataTransformer\CollectionToArrayTransformer;
use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener; use Symfony\Component\Form\Extension\Core\EventListener\MergeCollectionListener;
...@@ -22,6 +24,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; ...@@ -22,6 +24,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Routing\Router; use Symfony\Component\Routing\Router;
use Symfony\Component\Validator\Constraints\Count; use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\NotBlank;
/** Type de champ 'file_upload'. /** Type de champ 'file_upload'.
* *
...@@ -150,12 +153,19 @@ class FileUploadType extends AbstractType ...@@ -150,12 +153,19 @@ class FileUploadType extends AbstractType
} }
} elseif ($options['required']) { } elseif ($options['required']) {
$required = new \Symfony\Component\Validator\Constraints\NotBlank(['message' => 'file_upload.required']); $required = new NotBlank(['message' => 'file_upload.required']);
$constraints[] = $required; $constraints[] = $required;
} }
if ($options['min_file_size'] || $options['max_file_size'] || $options['accept_file_types']) { if ($options['min_file_size'] || $options['max_file_size']) {
// @TODO gère les tailles $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; return $constraints;
......
<?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'];
}
}
<?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();
}
}
<?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';
}
<?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();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment