From f4db0491e751ca48998e81b0f407d1f052e39b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Frank=20Br=C3=BCckner?= <dev@froschdesignstudio.de> Date: Tue, 7 Nov 2023 18:32:23 +0100 Subject: [PATCH 1/2] Adds unit test for isFloat validator that demonstrates an error with empty strings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Frank Brückner <dev@froschdesignstudio.de> --- test/Validator/IsFloatTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/Validator/IsFloatTest.php b/test/Validator/IsFloatTest.php index 88cead26..8a740804 100644 --- a/test/Validator/IsFloatTest.php +++ b/test/Validator/IsFloatTest.php @@ -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'] + ); + } } From c42d9e506703a53130d81aa39375c2130a5e95e3 Mon Sep 17 00:00:00 2001 From: George Steel <george@net-glue.co.uk> Date: Tue, 7 Nov 2023 21:19:29 +0000 Subject: [PATCH 2/2] Check for an empty string early Signed-off-by: George Steel <george@net-glue.co.uk> --- src/Validator/IsFloat.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Validator/IsFloat.php b/src/Validator/IsFloat.php index 0e662693..6c142563 100644 --- a/src/Validator/IsFloat.php +++ b/src/Validator/IsFloat.php @@ -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);