Skip to content

Commit

Permalink
Add missing validation methods for Number property
Browse files Browse the repository at this point in the history
  • Loading branch information
mcaskill committed Oct 31, 2019
1 parent 7acaa1a commit 5515529
Showing 1 changed file with 47 additions and 2 deletions.
49 changes: 47 additions & 2 deletions src/Charcoal/Property/NumberProperty.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ public function max()
return $this->max;
}


/**
* The property's default validation methods.
*
Expand All @@ -94,10 +93,56 @@ public function validationMethods()

return array_merge($parentMethods, [
'max',
'min'
'min',
]);
}

/**
* @return boolean
*/
public function validateRequired()
{
if ($this['required'] && !is_numeric($this->val())) {
$this->validator()->error('Value is required.', 'required');

return false;
}

return true;
}

/**
* @return boolean
*/
public function validateMin()
{
$min = $this->min();
if (!$min) {
return true;
}
$valid = ($this->val() >= $min);
if ($valid === false) {
$this->validator()->error('The number is smaller than the minimum value', 'min');
}
return $valid;
}

/**
* @return boolean
*/
public function validateMax()
{
$max = $this->max();
if (!$max) {
return true;
}
$valid = ($this->val() <= $max);
if ($valid === false) {
$this->validator()->error('The number is bigger than the maximum value', 'max');
}
return $valid;
}

/**
* Get the SQL type (Storage format)
*
Expand Down

0 comments on commit 5515529

Please sign in to comment.