Skip to content

Commit c8dc6f1

Browse files
committed
feat(input): add float typed accessor
- Add InputData::float() for numeric input values - Cover float strings, integers, defaults, nulls, and invalid values - Document float access with matching decimal validation examples Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
1 parent a6350cd commit c8dc6f1

5 files changed

Lines changed: 70 additions & 3 deletions

File tree

system/Input/InputData.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,34 @@ public function integer(string $key, ?int $default = null): ?int
9696
throw $this->invalidType($key, 'integer');
9797
}
9898

99+
/**
100+
* Returns an input field as a float.
101+
*
102+
* Supports dot-array syntax for nested input data.
103+
*/
104+
public function float(string $key, ?float $default = null): ?float
105+
{
106+
$value = $this->get($key, $default);
107+
108+
if ($value === null || is_float($value)) {
109+
return $value;
110+
}
111+
112+
if (is_int($value)) {
113+
return (float) $value;
114+
}
115+
116+
if (is_string($value)) {
117+
$float = filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE);
118+
119+
if ($float !== null) {
120+
return $float;
121+
}
122+
}
123+
124+
throw $this->invalidType($key, 'float');
125+
}
126+
99127
/**
100128
* Returns an input field as a boolean.
101129
*

tests/system/Input/InputDataTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,37 @@ public function testIntegerThrowsForInvalidInputValue(): void
114114
$input->integer('page');
115115
}
116116

117+
public function testFloatReturnsInputFloat(): void
118+
{
119+
$input = new InputData(['price' => '15.50']);
120+
121+
$this->assertEqualsWithDelta(15.50, $input->float('price'), PHP_FLOAT_EPSILON);
122+
}
123+
124+
public function testFloatReturnsInputIntegerAsFloat(): void
125+
{
126+
$input = new InputData(['price' => 15]);
127+
128+
$this->assertEqualsWithDelta(15.0, $input->float('price'), PHP_FLOAT_EPSILON);
129+
}
130+
131+
public function testFloatReturnsDefaultForMissingInputField(): void
132+
{
133+
$input = new InputData([]);
134+
135+
$this->assertEqualsWithDelta(1.5, $input->float('price', 1.5), PHP_FLOAT_EPSILON);
136+
}
137+
138+
public function testFloatThrowsForInvalidInputValue(): void
139+
{
140+
$input = new InputData(['price' => 'free']);
141+
142+
$this->expectException(InvalidArgumentException::class);
143+
$this->expectExceptionMessage('The input "price" value cannot be read as float.');
144+
145+
$input->float('price');
146+
}
147+
117148
public function testBooleanReturnsInputBoolean(): void
118149
{
119150
$input = new InputData(['active' => 'true']);
@@ -174,12 +205,14 @@ public function testTypedAccessorsReturnNullForNullInputFields(): void
174205
$input = new InputData([
175206
'title' => null,
176207
'page' => null,
208+
'price' => null,
177209
'active' => null,
178210
'tags' => null,
179211
]);
180212

181213
$this->assertNull($input->string('title', 'Untitled'));
182214
$this->assertNull($input->integer('page', 1));
215+
$this->assertNull($input->float('price', 1.5));
183216
$this->assertNull($input->boolean('active', false));
184217
$this->assertNull($input->array('tags', ['draft']));
185218
}

user_guide_src/source/incoming/form_requests/015.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
$input = $request->validatedInput();
66

77
$page = $input->integer('page', 1);
8+
$rating = $input->float('rating', 0.0);
89
$active = $input->boolean('active', false);
910
$publishedAt = $input->date('published_at', 'Y-m-d');
1011
$status = $input->enum('status', PostStatus::class, PostStatus::DRAFT);

user_guide_src/source/libraries/validation.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@ Typed Validated Input
486486
``getValidatedInput()`` returns the same validated data as a
487487
``CodeIgniter\Validation\ValidatedInput`` object. Use it after validation
488488
succeeds when you want to read common controller values as strings, integers,
489-
booleans, arrays, dates, or enums:
489+
floats, booleans, arrays, dates, or enums:
490490

491491
.. versionadded:: 4.8.0
492492

@@ -514,6 +514,8 @@ All methods support dot-array syntax for nested validated data.
514514
missing, it returns the default value or ``null``.
515515
* ``integer($key, $default = null)`` returns ``int|null``. If the field is
516516
missing, it returns the default value or ``null``.
517+
* ``float($key, $default = null)`` returns ``float|null``. If the field is
518+
missing, it returns the default value or ``null``.
517519
* ``boolean($key, $default = null)`` returns ``bool|null``. If the field is
518520
missing, it returns the default value or ``null``.
519521
* ``array($key, $default = null)`` returns ``array|null``. If the field is
@@ -529,8 +531,8 @@ Fields that are present with a ``null`` value return ``null``. This lets you
529531
distinguish a missing optional field from a field that was validated as
530532
``null``.
531533

532-
Use validation rules such as ``integer``, ``valid_date``, ``in_list``, or a
533-
custom rule to ensure the value matches the type you plan to read. The
534+
Use validation rules such as ``integer``, ``decimal``, ``valid_date``,
535+
``in_list``, or a custom rule to ensure the value matches the type you plan to read. The
534536
``date()`` method only parses the value; validation rules should enforce
535537
acceptable date formats and ranges. For strict calendar validation, add a rule
536538
such as ``valid_date[Y-m-d]``.

user_guide_src/source/libraries/validation/048.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
$validation->setRules([
77
'title' => 'required|string',
88
'page' => 'permit_empty|integer',
9+
'rating' => 'permit_empty|decimal',
910
'active' => 'permit_empty|in_list[0,1,true,false,yes,no,on,off]',
1011
'tags' => 'permit_empty|is_array',
1112
'published_at' => 'permit_empty|valid_date[Y-m-d]',
@@ -15,6 +16,7 @@
1516
$data = [
1617
'title' => 'Hello World',
1718
'page' => '2',
19+
'rating' => '4.5',
1820
'active' => 'true',
1921
'tags' => ['php', 'codeigniter'],
2022
'published_at' => '2026-05-04',
@@ -30,6 +32,7 @@
3032

3133
$title = $input->string('title');
3234
$page = $input->integer('page', 1);
35+
$rating = $input->float('rating', 0.0);
3336
$active = $input->boolean('active', false);
3437
$tags = $input->array('tags', []);
3538
$publishedAt = $input->date('published_at', 'Y-m-d');

0 commit comments

Comments
 (0)