Skip to content

Commit

Permalink
Merge branch 'hotfix/15_empty_validation_rule'
Browse files Browse the repository at this point in the history
  • Loading branch information
airmoi committed Aug 2, 2017
2 parents 3f8c5e0 + d765fdd commit 6b43d22
Show file tree
Hide file tree
Showing 3 changed files with 342 additions and 129 deletions.
4 changes: 4 additions & 0 deletions src/FileMakerValidationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class FileMakerValidationException extends FileMakerException
*/
public function addError(Field $field, $rule, $value)
{
if (is_array($value)) {
$value = print_r($value, true);
}

$message = self::getValidationErrorString($field, $rule, $value);
$this->errors[] = [$field, $rule, $value, $message];
$messages = empty($this->getMessage()) ? [] : explode("\n", $this->getMessage());
Expand Down
53 changes: 43 additions & 10 deletions src/Object/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,28 @@ public function validate($value)
$isValid = false;
$validationError = new FileMakerValidationException($this->layout->fm);

//stops everything when invalid data is provided
if (!self::isValideValue($value)) {
$validationError->addError($this, null, $value);
throw $validationError;
}

foreach ($this->getValidationRules() as $rule) {
switch ($rule) {
case FileMaker::RULE_NOTEMPTY:
if (empty($value)) {
if (self::isEmpty($value)) {
$validationError->addError($this, $rule, $value);
}
break;
case FileMaker::RULE_NUMERICONLY:
if (!empty($value)) {
if (!self::isEmpty($value)) {
if ($this->checkNumericOnly($value)) {
$validationError->addError($this, $rule, $value);
}
}
break;
case FileMaker::RULE_MAXCHARACTERS:
if (!empty($value)) {
if (!self::isEmpty($value)) {
$strlen = strlen($value);
if ($strlen > $this->maxCharacters) {
$validationError->addError($this, $rule, $value);
Expand All @@ -130,7 +136,7 @@ public function validate($value)
break;
case FileMaker::RULE_TIMEOFDAY:
case FileMaker::RULE_TIME_FIELD:
if (!empty($value)) {
if (!self::isEmpty($value)) {
if (!$this->checkTimeFormat($value)) {
$validationError->addError($this, $rule, $value);
} else {
Expand All @@ -139,7 +145,7 @@ public function validate($value)
}
break;
case FileMaker::RULE_TIMESTAMP_FIELD:
if (!empty($value)) {
if (!self::isEmpty($value)) {
if (!$this->checkTimeStampFormat($value)) {
$validationError->addError($this, $rule, $value);
} else {
Expand All @@ -149,7 +155,7 @@ public function validate($value)
}
break;
case FileMaker::RULE_DATE_FIELD:
if (!empty($value)) {
if (!self::isEmpty($value)) {
if (!$this->checkDateFormat($value)) {
$validationError->addError($this, $rule, $value);
} else {
Expand All @@ -158,7 +164,7 @@ public function validate($value)
}
break;
case FileMaker::RULE_FOURDIGITYEAR:
if (!empty($value)) {
if (!self::isEmpty($value)) {
switch ($this->result) {
case 'timestamp':
if ($this->checkTimeStampFormatFourDigitYear($value)) {
Expand Down Expand Up @@ -451,7 +457,7 @@ public function getStyleType()
public function checkTimeStampFormatFourDigitYear($value)
{
return preg_match(
'#^[ ]*([0-9]{1,2})[-,/,\\\\]([0-9]{1,2})[-,/,\\\\]([0-9]{4})[ ]*([0-9]{1,2})[:]([0-9]{1,2})([:][0-9]{1,2})?([ ]*((AM|PM)|(am|pm)))?[ ]*$#',
'#^[ ]*([0-9]{1,2})[-,/,\\\\]([0-9]{1,2})[-,/,\\\\]([0-9]{4})[ ]*([0-9]{1,2})[:]([0-9]{1,2})([:][0-9]{1,2})?([ ]*((AM|PM)|(am|pm)))?[ ]*$#i',
$value
);
}
Expand All @@ -466,7 +472,7 @@ public function checkTimeStampFormatFourDigitYear($value)
public function checkTimeStampFormat($value)
{
return preg_match(
'#^[ ]*([0-9]{1,2})[-,/,\\\\]([0-9]{1,2})([-,/,\\\\]([0-9]{1,4}))?[ ]*([0-9]{1,2})[:]([0-9]{1,2})([:][0-9]{1,2})?([ ]*((AM|PM)|(am|pm)))?[ ]*$#',
'#^[ ]*([0-9]{1,2})[-,/,\\\\]([0-9]{1,2})([-,/,\\\\]([0-9]{1,4}))?[ ]*([0-9]{1,2})[:]([0-9]{1,2})([:][0-9]{1,2})?([ ]*((AM|PM)|(am|pm)))?[ ]*$#i',
$value
);
}
Expand All @@ -492,7 +498,7 @@ public function checkDateFormat($value)
*/
public function checkTimeFormat($value)
{
return preg_match('#^[ ]*([0-9]{1,2})[:]([0-9]{1,2})([:][0-9]{1,2})?([ ]*((AM|PM)|(am|pm)))?[ ]*$#', $value);
return preg_match('#^[ ]*([0-9]{1,2})[:]([0-9]{1,2})([:][0-9]{1,2})?([ ]*((AM|PM)|(am|pm)))?[ ]*$#i', $value);
}

/**
Expand Down Expand Up @@ -565,4 +571,31 @@ public function checkTimeValidity($value, $rule, FileMakerValidationException $v
}
}
}

/**
* @param string $value check if value is a non empty string("", null, true, false, [])
*
* @return bool
*/
private static function isEmpty($value)
{
if(is_null($value) || $value === '') {
return true;
}
return false;
}

/**
* @param string $value check if value is a valid FileMaker data("", null, true, false, [])
*
* @return bool
*/
private static function isValideValue($value)
{
if (!is_string($value) && !is_numeric($value) && !is_bool($value) && !is_null($value)) {
return false;
}

return true;
}
}
Loading

0 comments on commit 6b43d22

Please sign in to comment.