Browse Source

validator, #16

git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@145 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
master
pzinovkin 15 years ago
parent
commit
708534a092
  1. 4
      form/FormViewHelper.php
  2. 39
      validator/EqualValidator.php
  3. 5
      validator/Validator.php

4
form/FormViewHelper.php

@ -26,12 +26,12 @@ class FormViewHelper extends ViewHelper
return $this; return $this;
} }
public function value($field)
public function value($field, $default = '')
{ {
if (isset($this->data['values'][$field])) { if (isset($this->data['values'][$field])) {
return $this->view->escape($this->data['values'][$field]); return $this->view->escape($this->data['values'][$field]);
} }
return '';
return $this->view->escape($default);
} }
public function message($field) public function message($field)

39
validator/EqualValidator.php

@ -0,0 +1,39 @@
<?php
/**
* @copyright NetMonsters <team@netmonsters.ru>
* @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;
}
}

5
validator/Validator.php

@ -29,8 +29,11 @@ abstract class Validator implements iValidator
$this->message = null; $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; $this->templates[$key] = $message;
} }

Loading…
Cancel
Save