8000 UploadValidatorExtension.php · GitHub
[go: up one dir, main page]

Skip to content

Instantly share code, notes, and snippets.

@aitboudad
Created April 16, 2016 18:15
Show Gist options
  • Save aitboudad/02db0ef89300991503fd287fb6ce3eec to your computer and use it in GitHub Desktop.
Save aitboudad/02db0ef89300991503fd287fb6ce3eec to your computer and use it in GitHub Desktop.
services:
app.upload_type_extension:
class: Symfony\Component\Form\Extension\Validator\Type\UploadValidatorExtension
arguments: ['@translator', '%validator.translation_domain%']
tags:
- { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Form\Extension\Validator\Type;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Translation\TranslatorInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
*/
class UploadValidatorExtension extends AbstractTypeExtension
{
private $translator;
private $translationDomain;
/**
* @param TranslatorInterface $translator The translator for translating error messages
* @param null|string $translationDomain The translation domain for translating
*/
public function __construct(TranslatorInterface $translator, $translationDomain = null)
{
$this->translator = $translator;
$this->translationDomain = $translationDomain;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setNormalizer('post_max_size_message', function (Options $options, $errorMessage) {
return $this->translator->trans($errorMessage, array(), $this->translationDomain);
});
}
/**
* {@inheritdoc}
*/
public function getExtendedType()
{
return FormType::class;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
0