Compare commits

..

2 Commits

Author SHA1 Message Date
Steve Boyd
4d5a5a15a2
Merge a8c3aa39b0 into 6bb9a0b33d 2024-10-15 05:24:42 +00:00
Steve Boyd
a8c3aa39b0 NEW Validate DBFields 2024-10-15 18:24:34 +13:00
3 changed files with 13 additions and 18 deletions

View File

@ -43,11 +43,6 @@ class StringFieldValidator extends FieldValidator
$result->addFieldError($this->name, $message, value: $this->value);
return $result;
}
// TODO this seems non-sensical - should enforce minLength??
// Blank strings are valid, even if there's a minLength requirement
if ($this->value === '') {
return $result;
}
$len = mb_strlen($this->value);
if (!is_null($this->minLength) && $len < $this->minLength) {
$message = _t(

View File

@ -30,7 +30,7 @@ class DBDecimal extends DBField
/**
* Create a new Decimal field.
*/
public function __construct(?string $name = null, ?int $wholeSize = 9, ?int $decimalSize = 2, float|int $defaultValue = 0)
public function __construct(?string $name = null, ?int $wholeSize = 9, ?int $decimalSize = 2, float|int $defaultValue = 0.0)
{
$this->wholeSize = is_int($wholeSize) ? $wholeSize : 9;
$this->decimalSize = is_int($decimalSize) ? $decimalSize : 2;
@ -113,23 +113,23 @@ class DBDecimal extends DBField
public function nullValue(): int
{
return 0;
return 0.0;
}
public function prepValueForDB(mixed $value): array|float|int|null
{
if ($value === true) {
return 1;
return 1.0;
}
if (empty($value) || !is_numeric($value)) {
return 0;
return 0.0;
}
if (abs((float) $value - (int) $value) < PHP_FLOAT_EPSILON) {
return (int)$value;
return (int) $value;
}
return (float)$value;
return (float) $value;
}
}

View File

@ -26,13 +26,6 @@ class StringFieldValidatorTest extends SapphireTest
'exception' => false,
'expected' => true,
],
'valid-blank-when-min' => [
'value' => '',
'minLength' => 5,
'maxLength' => null,
'exception' => false,
'expected' => true,
],
'valid-max' => [
'value' => 'fish',
'minLength' => 0,
@ -75,6 +68,13 @@ class StringFieldValidatorTest extends SapphireTest
'exception' => false,
'expected' => false,
],
'invalid-blank-with-min' => [
'value' => '',
'minLength' => 5,
'maxLength' => null,
'exception' => false,
'expected' => false,
],
'invalid-above-min' => [
'value' => 'fish',
'minLength' => 0,