Moving translation logic to the validator (fixes for 2.2, see #52)

This commit is contained in:
Gregwar
2013-03-03 13:54:31 +01:00
parent 1fef229c8f
commit 4e13d2d4cb
3 changed files with 20 additions and 3 deletions

View File

@ -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);