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