From 4e13d2d4cb79c451ffdacb643701baa8547b3041 Mon Sep 17 00:00:00 2001 From: Gregwar Date: Sun, 3 Mar 2013 13:54:31 +0100 Subject: [PATCH] Moving translation logic to the validator (fixes for 2.2, see #52) --- Resources/config/services.yml | 1 + Type/CaptchaType.php | 10 +++++++++- Validator/CaptchaValidator.php | 12 ++++++++++-- 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/Resources/config/services.yml b/Resources/config/services.yml index d675854..64827f0 100755 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -4,6 +4,7 @@ services: arguments: - @session - @gregwar_captcha.generator + - @translator - %gregwar_captcha.config% tags: - { name: form.type, alias: captcha } diff --git a/Type/CaptchaType.php b/Type/CaptchaType.php index 6b84f7f..2a90ed7 100644 --- a/Type/CaptchaType.php +++ b/Type/CaptchaType.php @@ -9,6 +9,7 @@ use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Form\FormEvents; +use Symfony\Component\Translation\TranslatorInterface; use Gregwar\CaptchaBundle\Validator\CaptchaValidator; use Gregwar\CaptchaBundle\Generator\CaptchaGenerator; @@ -36,6 +37,11 @@ class CaptchaType extends AbstractType */ protected $generator; + /** + * @var TranslatorInterface + */ + protected $translator; + /** * Options * @var array @@ -47,10 +53,11 @@ class CaptchaType extends AbstractType * @param \Gregwar\CaptchaBundle\Generator\CaptchaGenerator $generator * @param array $options */ - public function __construct(SessionInterface $session, CaptchaGenerator $generator, $options) + public function __construct(SessionInterface $session, CaptchaGenerator $generator, TranslatorInterface $translator, $options) { $this->session = $session; $this->generator = $generator; + $this->translator = $translator; $this->options = $options; } @@ -63,6 +70,7 @@ class CaptchaType extends AbstractType $this->key = 'gcb_'.$builder->getForm()->getName(); $validator = new CaptchaValidator( + $this->translator, $this->session, $this->key, $options['invalid_message'], diff --git a/Validator/CaptchaValidator.php b/Validator/CaptchaValidator.php index 62beac3..6e498d9 100644 --- a/Validator/CaptchaValidator.php +++ b/Validator/CaptchaValidator.php @@ -5,6 +5,7 @@ namespace Gregwar\CaptchaBundle\Validator; use Symfony\Component\HttpFoundation\Session\SessionInterface; use Symfony\Component\Form\FormError; use Symfony\Component\Form\FormEvent; +use Symfony\Component\Translation\TranslatorInterface; /** * Captcha validator @@ -39,14 +40,21 @@ class CaptchaValidator */ private $humanity; + /** + * Translator + * @var TranslatorInterface + */ + private $translator; + /** * @param \Symfony\Component\HttpFoundation\Session\SessionInterface $session * @param string $key * @param string $invalidMessage * @param string|null $bypassCode */ - public function __construct(SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity) + public function __construct(TranslatorInterface $translator, SessionInterface $session, $key, $invalidMessage, $bypassCode, $humanity) { + $this->translator = $translator; $this->session = $session; $this->key = $key; $this->invalidMessage = $invalidMessage; @@ -73,7 +81,7 @@ class CaptchaValidator } if (!($code && is_string($code) && ($this->compare($code, $expectedCode) || $this->compare($code, $this->bypassCode)))) { - $form->addError(new FormError($this->invalidMessage)); + $form->addError(new FormError($this->translator->trans($this->invalidMessage, array(), 'validators'))); } else { if ($this->humanity > 0) { $this->updateHumanity($this->humanity);