From 708534a092d190a56b800c09af24ab6e121232db Mon Sep 17 00:00:00 2001 From: pzinovkin Date: Tue, 27 Apr 2010 23:03:54 +0000 Subject: [PATCH] validator, #16 git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@145 4cb57b5f-5bbd-dd11-951b-001d605cbbc5 --- form/FormViewHelper.php | 4 ++-- validator/EqualValidator.php | 39 +++++++++++++++++++++++++++++++++++++++ validator/Validator.php | 5 ++++- 3 files changed, 45 insertions(+), 3 deletions(-) create mode 100644 validator/EqualValidator.php diff --git a/form/FormViewHelper.php b/form/FormViewHelper.php index 48aa07d..a1186f8 100644 --- a/form/FormViewHelper.php +++ b/form/FormViewHelper.php @@ -26,12 +26,12 @@ class FormViewHelper extends ViewHelper return $this; } - public function value($field) + public function value($field, $default = '') { if (isset($this->data['values'][$field])) { return $this->view->escape($this->data['values'][$field]); } - return ''; + return $this->view->escape($default); } public function message($field) diff --git a/validator/EqualValidator.php b/validator/EqualValidator.php new file mode 100644 index 0000000..d8e0120 --- /dev/null +++ b/validator/EqualValidator.php @@ -0,0 +1,39 @@ + + * @link http://netmonsters.ru + * @package Majestic + * @subpackage validator + * @since 2010-04-26 + * @version SVN: $Id$ + * @filesource $URL$ + */ + +class EqualValidator extends Validator +{ + + const NOT_EQUAL = 'not_match'; + + protected $templates = array(self::NOT_EQUAL => 'Tokens do not match'); + + protected $token; + + public function __construct($token) + { + $this->token = $token; + } + + public function isValid($value, $context = null) + { + $this->setValue($value); + if ($this->token === null) { + throw new Exception('Token not defined.'); + } + + if ($value !== $this->token) { + $this->error(); + return false; + } + return true; + } +} \ No newline at end of file diff --git a/validator/Validator.php b/validator/Validator.php index d201b3d..765a7f3 100644 --- a/validator/Validator.php +++ b/validator/Validator.php @@ -29,8 +29,11 @@ abstract class Validator implements iValidator $this->message = null; } - public function setMessage($key, $message) + public function setMessage($message, $key = null) { + if ($key === null) { + $key = current(array_keys($this->templates)); + } $this->templates[$key] = $message; }