Skip to content

Commit

Permalink
Merge pull request #110 from froschdesign/hotfix/isfloat-emptystring
Browse files Browse the repository at this point in the history
IsFloat validator should return a standard error message for empty strings
  • Loading branch information
froschdesign committed Nov 8, 2023
2 parents 4f23433 + c42d9e5 commit dafb5ed
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Validator/IsFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,12 @@ public function isValid($value)
return true;
}

if ($value === '') {
$this->error(self::NOT_FLOAT);

return false;
}

// Need to check if this is scientific formatted string. If not, switch to decimal.
$formatter = new NumberFormatter($this->getLocale(), NumberFormatter::SCIENTIFIC);

Expand Down
10 changes: 10 additions & 0 deletions test/Validator/IsFloatTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,4 +217,14 @@ public function testNotFloat(): void
$message = $this->validator->getMessages();
self::assertStringContainsString('does not appear to be a float', $message['notFloat']);
}

public function testEmptyStringShouldReturnStandardErrorMessage(): void
{
self::assertFalse($this->validator->isValid(''));
$message = $this->validator->getMessages();
self::assertStringContainsString(
'does not appear to be a float',
$message['notFloat']
);
}
}

0 comments on commit dafb5ed

Please sign in to comment.