Commit 1aedcd49 authored by Ad Schellevis's avatar Ad Schellevis

(mvc) move default validator into base type

parent 9910a79f
......@@ -67,6 +67,11 @@ class AuthenticationServerField extends BaseField
*/
private $internalMultiSelect = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "please specify a valid authentication server";
/**
* generate validation data (list of AuthServers)
*/
......@@ -158,26 +163,19 @@ class AuthenticationServerField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "please specify a valid authentication server";
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
$validators = parent::getValidators();
if ($this->internalValue != null) {
if ($this->internalMultiSelect) {
// field may contain more than one authentication server
return array(new CsvListValidator(array('message' => $msg,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey]))));
$validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
} else {
// single authentication server selection
return array(new InclusionIn(array('message' => $msg,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey]))));
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
}
} else {
// empty field and not required, skip this validation.
return array();
}
return $validators;
}
}
......@@ -43,6 +43,11 @@ class AutoNumberField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "invalid integer value";
/**
* maximum value for this field
* @var integer
......@@ -122,23 +127,15 @@ class AutoNumberField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "invalid integer value" ;
} else {
$msg = $this->internalValidationMessage;
}
$validators = parent::getValidators();
if (($this->internalIsRequired == true || $this->internalValue != null)) {
$result = array();
$result[] = new MinMaxValidator(array('message' => $msg,
if ($this->internalValue != null) {
$validators[] = new MinMaxValidator(array('message' => $this->internalValidationMessage,
"min" => $this->minimum_value,
"max" => $this->maximum_value
));
$result[] = new IntegerValidator(array('message' => $msg));
return $result;
} else {
// empty field and not required, skip this validation.
return array();
$validators[] = new IntegerValidator(array('message' => $this->internalValidationMessage));
}
return $validators;
}
}
......@@ -30,6 +30,8 @@
namespace OPNsense\Base\FieldTypes;
use Phalcon\Validation\Validator\PresenceOf;
/**
* Class BaseField
* @package OPNsense\Base\FieldTypes
......@@ -323,13 +325,30 @@ abstract class BaseField
return $this->internalChildnodes;
}
/**
* check if this field is unused and required
* @return bool
*/
protected function isEmptyAndRequired()
{
if ($this->internalIsRequired && ($this->internalValue == "" || $this->internalValue == null)) {
return true;
} else {
return false;
}
}
/**
* return field validators for this field
* @return array returns validators for this field type (empty if none)
*/
public function getValidators()
{
return array();
$validators = array();
if ($this->isEmptyAndRequired()) {
$validators[] = new PresenceOf(array('message' => $this->internalValidationMessage)) ;
}
return $validators;
}
/**
......
......@@ -41,6 +41,10 @@ class BooleanField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "value should be a boolean (0,1)";
/**
* retrieve field validators for this field type
......@@ -51,16 +55,11 @@ class BooleanField extends BaseField
// regexp for validating boolean values.
$regex_mask = "/^([0,1]){1}$/";
if ($this->internalValidationMessage == null) {
$msg = "value should be a boolean (0,1)" ;
} else {
$msg = $this->internalValidationMessage;
}
if ($this->internalIsRequired == true || $this->internalValue != null) {
return array(new Regex(array('message' => $msg,'pattern'=>trim($regex_mask))));
} else {
// empty field and not required, skip this validation.
return array();
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new Regex(array('message' => $this->internalValidationMessage,
'pattern'=>trim($regex_mask)));
}
return $validators;
}
}
......@@ -42,6 +42,11 @@ class CSVListField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "list validation error";
/**
* item separator
* @var string
......@@ -126,15 +131,11 @@ class CSVListField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "list validation error" ;
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired || $this->internalValue != null) && $this->internalMask != null) {
return array(new Regex(array('message' => $msg,'pattern'=>trim($this->internalMask))));
} else {
return array();
$validators = parent::getValidators();
if ($this->internalValue != null && $this->internalMask != null) {
$validators[] = new Regex(array('message' => $this->internalValidationMessage,
'pattern'=>trim($this->internalMask)));
}
return $validators;
}
}
......@@ -49,6 +49,11 @@ class CertificateField extends BaseField
*/
private $certificateType = "cert";
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "option not in list";
/**
* @var array collected options
*/
......@@ -110,16 +115,11 @@ class CertificateField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "option not in list" ;
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
return array(new InclusionIn(array('message' => $msg,'domain'=>array_keys(self::$internalOptionList))));
} else {
// empty field and not required, skip this validation.
return array();
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList)));
}
return $validators;
}
}
......@@ -48,6 +48,11 @@ class ConfigdActionsField extends BaseField
*/
private static $internalOptionList = array();
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "please specify a valid action";
/**
* @var array filters to use on the configd selection
*/
......@@ -151,19 +156,11 @@ class ConfigdActionsField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "please specify a valid action";
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
return array(new InclusionIn(array('message' => $msg,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey]))));
} else {
// empty field and not required, skip this validation.
return array();
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
}
return $validators;
}
}
......@@ -42,22 +42,21 @@ class EmailField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "email address invalid";
/**
* retrieve field validators for this field type
* @return array returns Email validator
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "email address invalid" ;
} else {
$msg = $this->internalValidationMessage;
}
if ($this->internalIsRequired == true || $this->internalValue != null) {
return array(new Email(array('message' => $msg)));
} else {
// empty field and not required, skip this validation.
return array();
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new Email(array('message' => $this->internalValidationMessage));
}
return $validators;
}
}
......@@ -42,6 +42,11 @@ class IntegerField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "invalid integer value";
/**
* maximum value for this field
* @var integer
......@@ -94,23 +99,14 @@ class IntegerField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "invalid integer value" ;
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
$result = array();
$result[] = new MinMaxValidator(array('message' => $msg,
$validators = parent::getValidators();
if ($this->internalValue != null) {
$result[] = new MinMaxValidator(array('message' => $this->internalValidationMessage,
"min" => $this->minimum_value,
"max" => $this->maximum_value
));
$result[] = new IntegerValidator(array('message' => $msg));
return $result;
} else {
// empty field and not required, skip this validation.
return array();
));
$result[] = new IntegerValidator(array('message' => $this->internalValidationMessage));
}
return $validators;
}
}
......@@ -45,6 +45,11 @@ class InterfaceField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "please specify a valid interface";
/**
* @var array collected options
*/
......@@ -160,30 +165,22 @@ class InterfaceField extends BaseField
/**
* retrieve field validators for this field type
* @return array returns Text/regex validator
* @return array returns validators
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "please specify a valid interface";
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
$validators = parent::getValidators();
if ($this->internalValue != null) {
if ($this->internalMultiSelect) {
// field may contain more than one interface
return array(new CsvListValidator(array('message' => $msg,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey]))));
$validators[] = new CsvListValidator(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
} else {
// single interface selection
return array(new InclusionIn(array('message' => $msg,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey]))));
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys(self::$internalOptionList[$this->internalCacheKey])));
}
} else {
// empty field and not required, skip this validation.
return array();
}
return $validators;
}
}
......@@ -42,6 +42,11 @@ class ModelRelationField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "option not in list";
/**
* @var array collected options
*/
......@@ -135,25 +140,17 @@ class ModelRelationField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "option not in list";
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)
) {
$validators = parent::getValidators();
if ($this->internalValue != null) {
if (array_key_exists($this->internalCacheKey, self::$internalOptionList) &&
count(self::$internalOptionList[$this->internalCacheKey]) > 0) {
return array(new InclusionIn(array('message' => $msg,
return array(new InclusionIn(array('message' => $this->internalValidationMessage,
'domain' => array_keys(self::$internalOptionList[$this->internalCacheKey]))));
} else {
return array(new InclusionIn(array('message' => $msg,
return array(new InclusionIn(array('message' => $this->internalValidationMessage,
'domain' => array())));
}
} else {
// empty field and not required, skip this validation.
return array();
}
return $validators;
}
}
......@@ -42,6 +42,11 @@ class NetworkField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "please specify a valid network segment or address (IPv4/IPv6) ";
/**
* always lowercase / trim networks
* @param string $value
......@@ -57,23 +62,13 @@ class NetworkField extends BaseField
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "please specify a valid network segment or address (IPv4/IPv6) " ;
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
if ($this->internalValue == "any") {
$validators = parent::getValidators();
if ($this->internalValue != null) {
if ($this->internalValue != "any") {
// accept any as target
return array();
} else {
return array(new NetworkValidator(array('message' => $msg)));
$validators[] = new NetworkValidator(array('message' => $this->internalValidationMessage));
}
} else {
// empty field and not required, skip this validation.
return array();
}
return $validators;
}
}
......@@ -42,6 +42,11 @@ class OptionField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "option not in list";
/**
* @var array valid options for this list
*/
......@@ -92,22 +97,18 @@ class OptionField extends BaseField
return $result;
}
/**
* retrieve field validators for this field type
* @return array returns InclusionIn validator
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "option not in list" ;
} else {
$msg = $this->internalValidationMessage;
}
if (($this->internalIsRequired == true || $this->internalValue != null)) {
return array(new InclusionIn(array('message' => $msg,'domain'=>array_keys($this->internalOptionList))));
} else {
// empty field and not required, skip this validation.
return array();
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new InclusionIn(array('message' => $this->internalValidationMessage,
'domain'=>array_keys($this->internalOptionList)));
}
return $validators;
}
}
......@@ -42,6 +42,11 @@ class TextField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "text validation error";
/**
* @var null|string validation mask (regex)
*/
......@@ -62,16 +67,12 @@ class TextField extends BaseField
*/
public function getValidators()
{
$validators = array();
if ($this->internalValidationMessage == null) {
$msg = "text validation error" ;
} else {
$msg = $this->internalValidationMessage;
}
if ($this->internalIsRequired && empty($this->internalValue)) {
$validators[] = new PresenceOf(array('message' => $msg)) ;
} elseif ($this->internalValue != null && $this->internalMask != null) {
$validators[] = new Regex(array('message' => $msg,'pattern'=>trim($this->internalMask)));
$validators = parent::getValidators();
if ($this->internalValue != null) {
if ($this->internalValue != null && $this->internalMask != null) {
$validators[] = new Regex(array('message' => $this->internalValidationMessage,
'pattern'=>trim($this->internalMask)));
}
}
return $validators;
}
......
......@@ -42,22 +42,21 @@ class UrlField extends BaseField
*/
protected $internalIsContainer = false;
/**
* @var string default validation message string
*/
protected $internalValidationMessage = "invalid url";
/**
* retrieve field validators for this field type
* @return array returns Url validator
*/
public function getValidators()
{
if ($this->internalValidationMessage == null) {
$msg = "invalid url" ;
} else {
$msg = $this->internalValidationMessage;
}
if ($this->internalIsRequired == true || $this->internalValue != null) {
return array(new UrlValidator(array('message' => $msg)));
} else {
// empty field and not required, skip this validation.
return array();
$validators = parent::getValidators();
if ($this->internalValue != null) {
$validators[] = new UrlValidator(array('message' => $this->internalValidationMessage));
}
return $validators;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment