feat: add typed FormRequest accessors#10158
feat: add typed FormRequest accessors#10158memleakd wants to merge 3 commits intocodeigniter4:4.8from
Conversation
- Add typed helpers for reading validated integer, boolean, date, and enum values - Keep accessors scoped to validated FormRequest data only - Document expected validation/accessor responsibilities - Cover defaults, null values, invalid values, dot syntax, and enum variants Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
|
Thanks, I like the idea. Typed access to validated data is useful and would make controller code nicer. However, I don't think these accessors should be added directly to I would prefer a design like this: $validation->getValidated(); // array, unchanged
$formRequest->validated(); // array, unchanged
$input = $validation->getValidatedInput();
$input = $formRequest->validatedInput();
$page = $input->integer('page', 1);
$active = $input->boolean('active', false);
$status = $input->enum('status', Status::class);This keeps BC, makes the feature available outside So I'm positive about the concept, but I think we should be careful with the design and not rush the implementation. I would like to hear what others think before we decide whether this should be reshaped into a dedicated |
|
Thanks, this makes sense to me. I originally kept the helpers directly on I’ll convert the PR to draft for now and wait for more feedback before reshaping it. I agree the dedicated Happy to rework it in that direction if everyone feel the same. |
|
I went ahead and reworked this in the
I also updated the docs, tests, and changelog/interface-change note around this design. I’ll mark the PR ready for review again.
|
- add ValidatedInput for typed access to validated data - expose ValidatedInput from Validation and FormRequest - keep FormRequest focused on request validation flow - update docs and tests for the new typed input API Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
71b130d to
d7067f3
Compare
Signed-off-by: memleakd <121398829+memleakd@users.noreply.github.com>
I would add |
However, I would be against adding methods directly to the request object: $this->request->integer('page', 1);That leaves too much ambiguity about the input source, creating the same problem we have with $this->request->getQueryInput()->integer('page', 1);
$this->request->getPostInput()->boolean('active', false);
$this->request->getPayloadInput()->enum('status', Status::class);To make this reusable, maybe the current namespace CodeIgniter\Input;
class InputData
{
public function get(string $key, mixed $default = null): mixed {}
public function has(string $key): bool {}
public function integer(string $key, ?int $default = null): ?int {}
public function boolean(string $key, ?bool $default = null): ?bool {}
public function date(string $key, ...): ?Time {}
public function enum(string $key, string $enumClass, ?UnitEnum $default = null): ?UnitEnum {}
public function string(string $key, ?string $default = null): ?string {}
public function array(string $key, ?array $default = null): ?array {}
}Then The important part is agreeing on the behavior for missing values, One more important thing - I don't think this PR needs to implement request-level input methods, but the class design should not make that reuse difficult. Thoughts? |
|
Sorry. I was thinking about the And to shorten the code, $input = $this->request->getPostInput();
$email = $input->string('email', '');
$status = $input->integer('status', 0);Typed default values should not return another scalar type. There should be no other types for |
|
#9482 If we go back, I was talking about a similar behavior - separate methods for types. Here we see why it was necessary. Yes, the PR is imperfect, but we just rejected this idea. |
Description
This PR proposes adding typed access to validated data through a dedicated
ValidatedInputobject.It builds on the new FormRequest feature while keeping FormRequest focused on its core responsibilities: validation rules, authorization, input preparation, and failure handling. Typed access now lives on an object that represents validated input, so the same feature is also available from the Validation service.
The same API is available after using the Validation service directly:
Why
This keeps controller code clearer after validation succeeds, without turning FormRequest into a growing collection of typed helper methods and without changing the existing array-based APIs.
validated()andgetValidated()continue returning arrays as before. The typed input object is an optional convenience for reading common validated values in the expected type.Behavior
The new
ValidatedInputobject provides:get()has()integer()boolean()date()enum()These methods read from validated data only. They do not replace validation rules. If a present value cannot be read as the requested type, an
InvalidArgumentExceptionis thrown.Missing optional fields return the provided default value, or
nullwhen no default is provided. Fields present with anullvalue returnnull.Notes
This adds
ValidationInterface::getValidatedInput(), so custom implementations of the interface will need to add the new method. The changelog lists this under interface changes.Docs were updated so FormRequest shows the concise controller usage, while the Validation docs provide the canonical
ValidatedInputreference and behavior details.Checklist: