Skip to content

Commit

Permalink
fix: allow comma separated float values in FloatField::_from_internal()
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredhendrickson13 committed Jul 6, 2024
1 parent dbd61d8 commit 21aa9cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ class FloatField extends RESTAPI\Core\Field {
/**
* Converts the field value to its representation form from it's internal pfSense configuration value.
* @param string $internal_value The internal value from the pfSense configuration.
* @return int The field value in its representation form.
* @return float|null The field value in its representation form.
*/
protected function _from_internal(mixed $internal_value): mixed {
protected function _from_internal(mixed $internal_value): ?float {
# Allow commas as the decimal separator, but convert them to points so it passes the is_numeric() check
$internal_value = str_replace(',', '.', $internal_value);

# Return the value as an integer if it's numeric
if (is_numeric($internal_value)) {
return floatval($internal_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,21 @@ class APIFieldsFloatFieldTestCase extends TestCase {
$this->assert_equals($test_string_field->value, 50.0);
}

/**
* Checks to ensure FloatField value's internal values can be comma separated decimals
*/
public function test_float_field_from_internal_comma_separated(): void
{
# Create an example float field and ensure its value is always an float
$test_string_field = new FloatField();
$test_string_field->from_internal('20,15');
$this->assert_equals($test_string_field->value, 20.15);
$test_string_field->from_internal('0,15144352');
$this->assert_equals($test_string_field->value, 0.15144352);
$test_string_field->from_internal('50');
$this->assert_equals($test_string_field->value, 50.0);
}

/**
* Checks that the NumericRangeValidator is always present for float Fields.
*/
Expand Down

0 comments on commit 21aa9cb

Please sign in to comment.