-
Notifications
You must be signed in to change notification settings - Fork 0
Working with input data
The validator does not require one special request object. Instead, it accepts several useful input shapes and normalises them into a simple key/value structure internally.
That makes it easy to use with plain PHP, with PHP.GT/Input, and with custom request wrappers.
Validator::validate() accepts:
- associative arrays
- iterable objects
- ordinary objects with public properties
- objects that implement an
asArray()method
In practice, all of these are valid:
$validator->validate($form, $_POST);$validator->validate($form, $input);$validator->validate($form, (object)[
"email" => "dev@example.com",
"name" => "Ada Lovelace"
]);If the application uses GT\Input\Input, we can pass it directly:
use GT\Input\Input;
function do_submit(Input $input):void {
$validator->validate($form, $input);
}That works because the validator can consume iterable objects and objects that expose array-like request data.
The validator matches submitted values to inputs by the name attribute.
<input name="email" type="email" required />$validator->validate($form, [
"email" => "dev@example.com",
]);If the field name and the submitted key do not match, the validator will treat the field as empty or missing.
Checkbox groups are commonly named like this:
<input type="checkbox" name="interest[]" value="php" />
<input type="checkbox" name="interest[]" value="css" />The submitted PHP data is usually:
[
"interest" => ["php", "css"],
]DomValidation normalises the name attribute by stripping the trailing [] when looking up the submitted input, so this works naturally.
That means:
- DOM name:
interest[] - submitted key:
interest
If the submitted value is an array, the validator converts each entry to a string internally.
That is mainly useful for checkbox groups and other repeated field names.
If an object is passed in, the validator checks it in this order:
- if it has an
asArray()method, that is used - if it is traversable, it is converted with
iterator_to_array() - otherwise its public properties are read
Non-scalar object properties are treated as empty strings.
This allows simple request DTOs to work without extra mapping glue.
Keep the submitted structure close to what the HTML form actually emits.
That way:
- required checks behave as expected
- checkbox arrays stay grouped correctly
- custom rules that inspect other fields are easier to write
Once the input is accepted and validation fails, the next useful topic is Understanding validation errors.
DomValidation is a separately maintained component of PHP.GT WebEngine.