[TYPO3-team-core-v5] Validation: should '123' be considered a valid float?
Bastian Waidelich
bastian at typo3.org
Sat Sep 27 13:46:35 CEST 2008
Karsten Dambekalns wrote:
Hi Karsten,
we've discussed this yesterday and I gave it another thought.
In type-safe languages like C#, the difference between float and int is
very important. When you multiply two or more integers, the result will
always be an int. Divisions always result in float numbers.
In PHP the whole thing behaves a bit weird:
var_dump(3 * 2) => int(6)
var_dump(3 / 2) => float(1.5)
var_dump(3.5 / 2) => float(1.75)
but:
var_dump(6 / 2) => int(3)
var_dump(3.5 * 2) => float(7)
is_float(3.5 * 2) => TRUE
is_float(7) => FALSE
> Imagine a "Weight Watchers" website. You need to enter your weight. The argument
> has been assigned a "float" validator.
>
> You enter | Valid?
> 87.5 | Yes
> 68.0 | Yes
> 100 | No
I guess, in this example the usability of that website wouldn't be very
good if the user wasn't allowed to enter "100". But if I do a bank
transfer via online banking, I'm glad that "10000" is not a valid input
for the amount ;)
An extended float validator might look like this (similar to Maltes
proposal):
class Float extends F3::FLOW3::Validation::AbstractValidator {
public function __construct($minDecimalPlaces = 0, $maxDecimalPlaces =
NULL) {
...
}
public function isValidProperty($propertyValue,
F3::FLOW3::Validation::Errors &$errors) {
...
}
}
by default ($minDecimalPlaces = 0, $maxDecimalPlaces not set) the
validator could just return
(string)$value === (string)((float)$value)
Otherwise you'd have to split the input into two pieces..
Bastian
More information about the TYPO3-team-core-v5
mailing list