Validator, captcha, form, #16
git-svn-id: svn+ssh://code.netmonsters.ru/svn/majestic/branches/evo@141 4cb57b5f-5bbd-dd11-951b-001d605cbbc5
This commit is contained in:
119
form/Form.php
Normal file
119
form/Form.php
Normal file
@ -0,0 +1,119 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage form
|
||||
* @since 2010-04-24
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
abstract class Form
|
||||
{
|
||||
|
||||
const SUCCESS = 'success';
|
||||
const ERROR = 'error';
|
||||
|
||||
protected $fields = array();
|
||||
protected $messages = array(self::SUCCESS => 'Form data valid',
|
||||
self::ERROR => 'Form data invalid');
|
||||
|
||||
protected $valid = true;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->init();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return FormField
|
||||
*/
|
||||
protected function addField($name, $message = false)
|
||||
{
|
||||
$this->fields[$name] = new FormField($message);
|
||||
return $this->fields[$name];
|
||||
}
|
||||
|
||||
public function isValid($data)
|
||||
{
|
||||
if (!is_array($data)) {
|
||||
throw new Exception(__CLASS__ . '::' . __METHOD__ . ' expects an array');
|
||||
}
|
||||
|
||||
foreach ($this->fields as $field_name => $field) {
|
||||
if (isset($data[$field_name])) {
|
||||
$this->valid &= $field->isValid($data[$field_name], $data);
|
||||
} else {
|
||||
$this->valid &= $field->isValid(null, $data);
|
||||
}
|
||||
}
|
||||
if (!$this->valid) {
|
||||
$this->fillHelperData();
|
||||
}
|
||||
return $this->valid;
|
||||
}
|
||||
|
||||
public function getMessages()
|
||||
{
|
||||
$messages = array();
|
||||
foreach ($this->fields as $name => $field) {
|
||||
if ($mess = $field->getMessage()) {
|
||||
$messages[$name] = $mess;
|
||||
}
|
||||
}
|
||||
return $messages;
|
||||
}
|
||||
|
||||
public function getValues()
|
||||
{
|
||||
$values = array();
|
||||
foreach ($this->fields as $key => $field) {
|
||||
if (!$field->isIgnored()) {
|
||||
$values[$key] = $field->getValue();
|
||||
}
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
public function getSourceValues()
|
||||
{
|
||||
$values = array();
|
||||
foreach ($this->fields as $key => $field) {
|
||||
$values[$key] = $field->getSourceValue();
|
||||
}
|
||||
return $values;
|
||||
}
|
||||
|
||||
public function getMessageType()
|
||||
{
|
||||
return ($this->valid) ? self::SUCCESS : self::ERROR;
|
||||
}
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->messages[$this->getMessageType()];
|
||||
}
|
||||
|
||||
public function setSuccessMessage($message)
|
||||
{
|
||||
$this->messages[self::SUCCESS] = (string) $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setErrorMessage($message)
|
||||
{
|
||||
$this->messages[self::ERROR] = (string) $message;
|
||||
return $this;
|
||||
}
|
||||
|
||||
protected function fillHelperData()
|
||||
{
|
||||
$data['messages'] = $this->getMessages();
|
||||
$data['values'] = $this->getSourceValues();
|
||||
Session::set(get_class($this), $data);
|
||||
}
|
||||
|
||||
abstract protected function init();
|
||||
}
|
173
form/FormField.php
Normal file
173
form/FormField.php
Normal file
@ -0,0 +1,173 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage form
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class FormField
|
||||
{
|
||||
|
||||
protected $validators = array();
|
||||
protected $filters = array();
|
||||
|
||||
/**
|
||||
* Used instead message of validator if defined.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $default_message = false;
|
||||
protected $message = false;
|
||||
protected $value;
|
||||
|
||||
/* Flags */
|
||||
protected $required = true;
|
||||
protected $ignored = false;
|
||||
|
||||
|
||||
public function __construct($default_message = false)
|
||||
{
|
||||
$this->default_message = $default_message;
|
||||
}
|
||||
|
||||
public function setRequired($flag)
|
||||
{
|
||||
$this->required = (bool) $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isRequired()
|
||||
{
|
||||
return $this->required;
|
||||
}
|
||||
|
||||
public function setIgnored($flag)
|
||||
{
|
||||
$this->ignored = (bool) $flag;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function isIgnored()
|
||||
{
|
||||
return $this->ignored;
|
||||
}
|
||||
|
||||
public function addValidators($validators)
|
||||
{
|
||||
foreach ($validators as $validator) {
|
||||
$this->addValidator($validator);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addValidator($validator)
|
||||
{
|
||||
if ($validator instanceof iValidator) {
|
||||
$name = get_class($validator);
|
||||
} elseif (is_string($validator)) {
|
||||
$name = $validator . 'Validator';
|
||||
$validator = new $name();
|
||||
} else {
|
||||
throw new Exception('Invalid validator provided to addValidator; must be string or iValidator');
|
||||
}
|
||||
$this->validators[$name] = $validator;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFilters($filters)
|
||||
{
|
||||
foreach ($filters as $filter) {
|
||||
$this->addFilter($filter);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function addFilter($filter)
|
||||
{
|
||||
if ($filter instanceof iFilter) {
|
||||
$name = get_class($filter);
|
||||
} elseif (is_string($filter)) {
|
||||
$name = $filter . 'Filter';
|
||||
$filter = new $name();
|
||||
} else {
|
||||
throw new Exception('Invalid filter provided to addFilter; must be string or iFilter');
|
||||
}
|
||||
$this->filters[$name] = $filter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getValue()
|
||||
{
|
||||
$value = $this->value;
|
||||
if (is_array($value)) {
|
||||
array_walk_recursive($value, array($this, 'filterValue'));
|
||||
} else {
|
||||
$this->filterValue($value, $value);
|
||||
}
|
||||
return $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* $value & $key for array_walk_recursive
|
||||
*
|
||||
* @param mixed $value
|
||||
* @param mixed $key
|
||||
*/
|
||||
protected function filterValue(&$value, &$key)
|
||||
{
|
||||
foreach ($this->filters as $filter) {
|
||||
$value = $filter->filter($value);
|
||||
}
|
||||
}
|
||||
|
||||
public function getSourceValue()
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function isValid($value, $context = null)
|
||||
{
|
||||
$this->value = $value;
|
||||
// filtered value here
|
||||
$value = $this->getValue();
|
||||
|
||||
$valid = true;
|
||||
|
||||
if ((($value === '') || ($value === null)) && !$this->isRequired()) {
|
||||
return $valid;
|
||||
}
|
||||
|
||||
foreach ($this->validators as $validator) {
|
||||
if (is_array($value)) {
|
||||
foreach ($value as $val) {
|
||||
if (!$validator->isValid($val, $context)) {
|
||||
$valid = false;
|
||||
if (!$this->default_message) {
|
||||
throw new Exception('Define default message for array fields');
|
||||
}
|
||||
$this->message = $this->default_message;
|
||||
}
|
||||
}
|
||||
if ($valid) {
|
||||
continue;
|
||||
}
|
||||
} elseif ($validator->isValid($value, $context)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$valid = false;
|
||||
$this->message = ($this->default_message) ? $this->default_message : $validator->getMessage();
|
||||
break;
|
||||
}
|
||||
return $valid;
|
||||
}
|
||||
|
||||
public function getMessage()
|
||||
{
|
||||
return $this->message;
|
||||
}
|
||||
}
|
44
form/FormViewHelper.php
Normal file
44
form/FormViewHelper.php
Normal file
@ -0,0 +1,44 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright NetMonsters <team@netmonsters.ru>
|
||||
* @link http://netmonsters.ru
|
||||
* @package Majestic
|
||||
* @subpackage Form
|
||||
* @since 2010-04-25
|
||||
* @version SVN: $Id$
|
||||
* @filesource $URL$
|
||||
*/
|
||||
|
||||
class FormViewHelper extends ViewHelper
|
||||
{
|
||||
|
||||
protected $data = null;
|
||||
|
||||
public function form($form = null)
|
||||
{
|
||||
if ($this->data === null) {
|
||||
if ($form == null) {
|
||||
throw new Exception('Form name required for helper init');
|
||||
}
|
||||
$this->data = Session::get($form, array());
|
||||
Session::del($form);
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function value($field)
|
||||
{
|
||||
if (isset($this->data['values'][$field])) {
|
||||
return $this->view->escape($this->data['values'][$field]);
|
||||
}
|
||||
return '';
|
||||
}
|
||||
|
||||
public function message($field)
|
||||
{
|
||||
if (isset($this->data['messages'][$field])) {
|
||||
return '<span class="error">' . $this->view->escape($this->data['messages'][$field]) . '</span>';
|
||||
}
|
||||
return '';
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user