Merge pull request #25 from jeremylivingston/messageconfig

Add invalid_message configuration option
This commit is contained in:
Grégoire Passault 2012-06-29 07:53:28 -07:00
commit ece2a68170
4 changed files with 22 additions and 14 deletions

View File

@ -31,6 +31,7 @@ class Configuration implements ConfigurationInterface
->scalarNode('gc_freq')->defaultValue(100)->end()
->scalarNode('expiration')->defaultValue(60)->end()
->scalarNode('quality')->defaultValue(15)->end()
->scalarNode('invalid_message')->defaultValue('Bad code value')->end()
->end()
;
return $treeBuilder;

View File

@ -125,6 +125,7 @@ You can define the following type option :
* **web_path**: absolute path to public web folder (default="%kernel.root_dir%/../web")
* **gc_freq**: frequency of garbage collection in fractions of 1 (default=100)
* **expiration**: maximum lifetime of captcha image files in minutes (default=60)
* **invalid_message**: error message displayed when an non-matching code is submitted (default="Bad code value")
Example :

2
Type/CaptchaType.php Executable file → Normal file
View File

@ -47,7 +47,7 @@ class CaptchaType extends AbstractType
$this->key = $builder->getForm()->getName();
$builder->addValidator(
new CaptchaValidator($this->session, $this->key)
new CaptchaValidator($this->session, $this->key, $options['invalid_message'])
);
}

View File

@ -24,10 +24,16 @@ class CaptchaValidator implements FormValidatorInterface
*/
private $key;
public function __construct(Session $session, $key)
/**
* Error message text for non-matching submissions
*/
private $invalidMessage;
public function __construct(Session $session, $key, $invalidMessage)
{
$this->session = $session;
$this->key = $key;
$this->invalidMessage = $invalidMessage;
}
public function validate(FormInterface $form)
@ -37,7 +43,7 @@ class CaptchaValidator implements FormValidatorInterface
if (!($code && $excepted_code && is_string($code) && is_string($excepted_code)
&& $this->niceize($code) == $this->niceize($excepted_code))) {
$form->addError(new FormError('Bad code value'));
$form->addError(new FormError($this->invalidMessage));
}
$this->session->remove($this->key);