|
@ -1,7 +1,7 @@ |
|
|
<?php |
|
|
<?php |
|
|
/** |
|
|
/** |
|
|
* @copyright NetMonsters <team@netmonsters.ru> |
|
|
* @copyright NetMonsters <team@netmonsters.ru> |
|
|
* @link http://netmonsters.ru |
|
|
|
|
|
|
|
|
* @link http://netmonsters.ru |
|
|
* @package Majestic |
|
|
* @package Majestic |
|
|
* @subpackage form |
|
|
* @subpackage form |
|
|
* @since 2010-04-25 |
|
|
* @since 2010-04-25 |
|
@ -11,51 +11,93 @@ |
|
|
|
|
|
|
|
|
class FormField |
|
|
class FormField |
|
|
{ |
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @var iValidator[] |
|
|
|
|
|
*/ |
|
|
protected $validators = array(); |
|
|
protected $validators = array(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @var iFilter[] |
|
|
|
|
|
*/ |
|
|
protected $filters = array(); |
|
|
protected $filters = array(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* Used instead message of validator if defined. |
|
|
* Used instead message of validator if defined. |
|
|
* |
|
|
|
|
|
|
|
|
* |
|
|
* @var string |
|
|
* @var string |
|
|
*/ |
|
|
*/ |
|
|
protected $default_message = false; |
|
|
protected $default_message = false; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @var string |
|
|
|
|
|
*/ |
|
|
protected $message = false; |
|
|
protected $message = false; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @var mixed |
|
|
|
|
|
*/ |
|
|
protected $value; |
|
|
protected $value; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Flags */ |
|
|
/* Flags */ |
|
|
|
|
|
/** |
|
|
|
|
|
* @var bool |
|
|
|
|
|
*/ |
|
|
protected $required = true; |
|
|
protected $required = true; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @var bool |
|
|
|
|
|
*/ |
|
|
protected $ignored = false; |
|
|
protected $ignored = false; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param bool|string $default_message |
|
|
|
|
|
*/ |
|
|
public function __construct($default_message = false) |
|
|
public function __construct($default_message = false) |
|
|
{ |
|
|
{ |
|
|
$this->default_message = $default_message; |
|
|
$this->default_message = $default_message; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param bool $flag |
|
|
|
|
|
* @return FormField |
|
|
|
|
|
*/ |
|
|
public function setRequired($flag) |
|
|
public function setRequired($flag) |
|
|
{ |
|
|
{ |
|
|
$this->required = (bool) $flag; |
|
|
$this->required = (bool) $flag; |
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @return bool |
|
|
|
|
|
*/ |
|
|
public function isRequired() |
|
|
public function isRequired() |
|
|
{ |
|
|
{ |
|
|
return $this->required; |
|
|
return $this->required; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param bool $flag |
|
|
|
|
|
* @return FormField |
|
|
|
|
|
*/ |
|
|
public function setIgnored($flag) |
|
|
public function setIgnored($flag) |
|
|
{ |
|
|
{ |
|
|
$this->ignored = (bool) $flag; |
|
|
$this->ignored = (bool) $flag; |
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @return bool |
|
|
|
|
|
*/ |
|
|
public function isIgnored() |
|
|
public function isIgnored() |
|
|
{ |
|
|
{ |
|
|
return $this->ignored; |
|
|
return $this->ignored; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param string[]|iValidator[] $validators |
|
|
|
|
|
* @return FormField |
|
|
|
|
|
*/ |
|
|
public function addValidators($validators) |
|
|
public function addValidators($validators) |
|
|
{ |
|
|
{ |
|
|
foreach ($validators as $validator) { |
|
|
foreach ($validators as $validator) { |
|
@ -63,7 +105,12 @@ class FormField |
|
|
} |
|
|
} |
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* @param string|iValidator $validator |
|
|
|
|
|
* @return FormField |
|
|
|
|
|
* @throws InitializationException |
|
|
|
|
|
*/ |
|
|
public function addValidator($validator) |
|
|
public function addValidator($validator) |
|
|
{ |
|
|
{ |
|
|
if ($validator instanceof iValidator) { |
|
|
if ($validator instanceof iValidator) { |
|
@ -77,7 +124,7 @@ class FormField |
|
|
$this->validators[$name] = $validator; |
|
|
$this->validators[$name] = $validator; |
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function addFilters($filters) |
|
|
public function addFilters($filters) |
|
|
{ |
|
|
{ |
|
|
foreach ($filters as $filter) { |
|
|
foreach ($filters as $filter) { |
|
@ -85,10 +132,10 @@ class FormField |
|
|
} |
|
|
} |
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function addFilter($filter) |
|
|
public function addFilter($filter) |
|
|
{ |
|
|
{ |
|
|
if ($filter instanceof iFilter) { |
|
|
|
|
|
|
|
|
if ($filter instanceof iFilter) { |
|
|
$name = get_class($filter); |
|
|
$name = get_class($filter); |
|
|
} elseif (is_string($filter)) { |
|
|
} elseif (is_string($filter)) { |
|
|
$name = $filter . 'Filter'; |
|
|
$name = $filter . 'Filter'; |
|
@ -99,7 +146,7 @@ class FormField |
|
|
$this->filters[$name] = $filter; |
|
|
$this->filters[$name] = $filter; |
|
|
return $this; |
|
|
return $this; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getValue() |
|
|
public function getValue() |
|
|
{ |
|
|
{ |
|
|
$value = $this->value; |
|
|
$value = $this->value; |
|
@ -110,10 +157,10 @@ class FormField |
|
|
} |
|
|
} |
|
|
return $value; |
|
|
return $value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* $value & $key for array_walk_recursive |
|
|
* $value & $key for array_walk_recursive |
|
|
* |
|
|
|
|
|
|
|
|
* |
|
|
* @param mixed $value |
|
|
* @param mixed $value |
|
|
* @param mixed $key |
|
|
* @param mixed $key |
|
|
*/ |
|
|
*/ |
|
@ -123,24 +170,24 @@ class FormField |
|
|
$value = $filter->filter($value); |
|
|
$value = $filter->filter($value); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getSourceValue() |
|
|
public function getSourceValue() |
|
|
{ |
|
|
{ |
|
|
return $this->value; |
|
|
return $this->value; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function isValid($value, $context = null) |
|
|
public function isValid($value, $context = null) |
|
|
{ |
|
|
{ |
|
|
$this->value = $value; |
|
|
$this->value = $value; |
|
|
// filtered value here
|
|
|
// filtered value here
|
|
|
$value = $this->getValue(); |
|
|
$value = $this->getValue(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$valid = true; |
|
|
$valid = true; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ((($value === '') || ($value === null)) && !$this->isRequired()) { |
|
|
if ((($value === '') || ($value === null)) && !$this->isRequired()) { |
|
|
return $valid; |
|
|
return $valid; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($this->validators as $validator) { |
|
|
foreach ($this->validators as $validator) { |
|
|
if (is_array($value)) { |
|
|
if (is_array($value)) { |
|
|
foreach ($value as $val) { |
|
|
foreach ($value as $val) { |
|
@ -158,14 +205,14 @@ class FormField |
|
|
} elseif ($validator->isValid($value, $context)) { |
|
|
} elseif ($validator->isValid($value, $context)) { |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$valid = false; |
|
|
$valid = false; |
|
|
$this->message = ($this->default_message) ? $this->default_message : $validator->getMessage(); |
|
|
$this->message = ($this->default_message) ? $this->default_message : $validator->getMessage(); |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
return $valid; |
|
|
return $valid; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public function getMessage() |
|
|
public function getMessage() |
|
|
{ |
|
|
{ |
|
|
return $this->message; |
|
|
return $this->message; |
|
|