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

FileUploadType: ajoute les contraintes de validation sur le nombre de fichiers.

Showing with 35 additions and 0 deletions
+35 -0
......@@ -17,8 +17,11 @@ use Symfony\Component\Form\Extension\Csrf\CsrfProvider\CsrfProviderInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
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\NotNull;
class FileUploadType extends AbstractType
{
......@@ -87,6 +90,38 @@ class FileUploadType extends AbstractType
$resolver->setDefaults(['max_chunk_size' => $this->maxChunkSize ?: 0]);
$resolver->setAllowedTypes(['max_chunk_size' => 'int']);
$resolver->setNormalizers(['constraints' => function(OptionsResolver $options, $constraints) {
return $this->addConstraints($options, $constraints);
}]);
}
protected function addConstraints(OptionsResolver $options, $constraints)
{
if($options['multiple']) {
$range = [];
if($options['required']) {
$range['min'] = 1;
$range['minMessage'] = 'file_upload.required';
}
if($options['max_number_of_files']) {
$range['max'] = $options['max_number_of_files'];
$range['maxMessage'] = 'file_upload.max_number_of_files';
}
if(!empty($range)) {
$constraints[] = new Count($range);
}
} elseif($options['required']) {
$required = new \Symfony\Component\Validator\Constraints\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
}
return $constraints;
}
protected function getWidgetDefaults()
......
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