Skip to content

Commit

Permalink
refactoring validation system to properly use configuration params.
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Adams committed Apr 4, 2020
1 parent 5bf8a9e commit b796b77
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 91 deletions.
26 changes: 22 additions & 4 deletions modules/base/classes/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
*/

class owa_validation {


// name of param being validated.
var $name;
// hold config
var $conf;

Expand All @@ -44,12 +46,28 @@ class owa_validation {

function __construct($conf = array()) {

if (array_key_exists('errorMsgTemplate', $conf)):
if (array_key_exists('errorMsgTemplate', $conf)) {
$this->errorMsgTemplate = $conf['errorMsgTemplate'];
endif;
}

if (array_key_exists('errorMsg', $conf)) {
$this->setErrorMessage( $conf['errorMsg'] );
}

$this->conf = $conf;

}


function setName( $name ) {

$this->name = $name;
}

function getName() {

return $this->name;
}

function validate() {

return false;
Expand Down
21 changes: 12 additions & 9 deletions modules/base/classes/validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,18 @@ function __construct() {
function addValidation($name, $value, $validation, $conf) {

// Construct validatation obj
$obj = $this->validationFactory($validation);
$obj = $this->validationFactory($validation, $conf);
$obj->setValues($value);
$obj->setConfigArray($conf);

$this->validations[] = array('name' => $name, 'obj' => $obj);

return;

$this->setValidation( $name, $obj );
}

function setValidation($name, $obj) {

// add name to validator
$obj->setName($name);
$this->validations[] = array('name' => $name, 'obj' => $obj);
return;
}

/**
Expand All @@ -100,20 +98,25 @@ function validationFactory($class_file, $conf = array()) {
function doValidations() {

foreach ($this->validations as $k) {

owa_coreAPI::debug('Validating '.$k['name']. ' with '. get_class( $k['obj'] ));
$k['obj']->validate();

if ($k['obj']->hasError === true) {
owa_coreAPI::debug('Validation failed.');
$this->hasErrors = true;
$this->errorMsgs[$k['name']] = $k['obj']->getErrorMsg();

if ( isset( $k['obj']->conf['stopOnError'] ) && $k['obj']->conf['stopOnError'] === true ) {

break;
}

} else {
owa_coreAPI::debug('Validation succeeded.');
}
//owa_coreAPI::debug( $this->getErrorMsgs() );
}

}

/**
Expand Down
5 changes: 0 additions & 5 deletions plugins/validations/emailAddress.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@

class owa_emailAddressValidation extends owa_validation {

function __construct() {

return parent::__construct();
}

function validate() {

$error = $this->getErrorMsg();
Expand Down
25 changes: 13 additions & 12 deletions plugins/validations/entityDoesNotExist.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,26 @@

class owa_entityDoesNotExistValidation extends owa_validation {

function __construct() {

return parent::__construct();
}


function validate() {

$entity = owa_coreAPI::entityFactory($this->getConfig('entity'));
$entity->getByColumn($this->getConfig('column'), $this->getValues());

$values = $this->getValues();

if ( $values ) {

$entity->getByColumn($this->getConfig('column'), $values );

} else {

$this->setErrorMessage('No entity value to check.');
$this->hasError();
}

$error = $this->getErrorMsg();

if (empty($error)) {

$this->setErrorMessage('An entity with that value already exists.');
}

Expand All @@ -53,12 +59,7 @@ function validate() {
if (!empty($id)) {
$this->hasError();
}

return;

}

}


?>
12 changes: 1 addition & 11 deletions plugins/validations/entityExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,7 @@
*/

class owa_entityExistsValidation extends owa_validation {

function __construct() {

return parent::__construct();
}


function validate() {

$entity = owa_coreAPI::entityFactory($this->getConfig('entity'));
Expand All @@ -52,12 +47,7 @@ function validate() {
if (empty($id)) {
$this->hasError();
}

return;

}

}


?>
9 changes: 2 additions & 7 deletions plugins/validations/required.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,15 @@
*/

class owa_requiredValidation extends owa_validation {

function __construct() {

return parent::__construct();
}


function validate() {

$value = $this->getValues();

$error = $this->getErrorMsg();

if (empty($error)) {
$this->setErrorMessage('Required field was empty.');
$this->setErrorMessage( $this->getName().' is required.');
}

if (empty($value)):
Expand Down
14 changes: 4 additions & 10 deletions plugins/validations/stringLength.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@

class owa_stringLengthValidation extends owa_validation {

function __construct() {

return parent::__construct();
}

function validate() {

$value = $this->getValues();
Expand All @@ -52,34 +47,33 @@ function validate() {

case '<':
if (strlen($value) >= $length) {

$this->hasError();
}
break;

case '>':
if (strlen($value) <= $length) {

$this->hasError();
}
break;

case '<=':
if (strlen($value) > $length) {

$this->hasError();
}
break;

case '>=':
if (strlen($value) < $length) {

$this->hasError();
}
break;

}

return;

}

}


Expand Down
11 changes: 1 addition & 10 deletions plugins/validations/stringMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,6 @@

class owa_stringMatchValidation extends owa_validation {

function __construct() {

return parent::__construct();
}


function validate() {

$values_array = $this->getValues();
Expand All @@ -48,15 +42,12 @@ function validate() {
$this->setErrorMessage('Strings do not match.');
}

// validation logic
// validation logic
if ($string1 === $string2) {
;
} else {
$this->hasError();
}

return;

}

}
Expand Down
5 changes: 0 additions & 5 deletions plugins/validations/subStringMatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@

class owa_subStringMatchValidation extends owa_validation {

function __construct() {

return parent::__construct();
}

function validate() {

$value = $this->getValues();
Expand Down
21 changes: 8 additions & 13 deletions plugins/validations/subStringPosition.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@

class owa_subStringPositionValidation extends owa_validation {

function __construct() {

return parent::__construct();
}

function validate() {

$value = $this->getValues();
Expand All @@ -45,7 +40,7 @@ function validate() {

$operator = $this->getConfig('operator');
$position = $this->getConfig('position');

$verb = '';
switch ($operator) {

case "=":
Expand All @@ -54,6 +49,7 @@ function validate() {
;
} else {
$this->hasError();
$verb = 'was not';
}


Expand All @@ -63,19 +59,18 @@ function validate() {

if ($pos === $position) {
$this->hasError();
$verb = 'was';
}

break;
}

$error = $this->getErrorMsg();

if (empty($error)) {
$error = $this->setErrorMessage(sprintf('The string "%s" was found within the value at position %d', $substring, $pos));
// check to see if an error msg is passed from the controller
if ( ! $this->getErrorMsg() ) {
// if not set this default msg.
$this->setErrorMessage(sprintf('The string "%s" %s found within the value at position %d', $substring, $verb, $pos));
}




}

}
Expand Down
5 changes: 0 additions & 5 deletions plugins/validations/userName.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,6 @@

class owa_userNameValidation extends owa_validation {

function __construct() {

return parent::__construct();
}

function validate() {

$error = $this->getErrorMsg();
Expand Down

0 comments on commit b796b77

Please sign in to comment.