Skip to content

[RFC] Aggregate Validation #152

@gsteel

Description

@gsteel

Feature Request

It's often necessary to validate more than one field at a time, for example to make sure that the input for 2 fields is identical (amongst many other use-cases for conditional validation).

Normally, you'd attach a callback validator to the field you wish to fail but this has some drawbacks:

  • At the time the callback is called, you can't be sure that both fields are initially valid because the order of validation (fields) is not guaranteed. This means that you have to perform redundant, basic validation of both fields again before you can get on with the main purpose of the callback. This is a bigger problem, the more fields are involved with the aggregate validation.
  • Using callbacks like this gets messy - if you want to correctly assign the validation failure to a specific input, you need to pass in references to whatever you haven't bound to - i.e. Bind the callback to nothing (default), the InputFilter ($this), the validator instance, a single input etc - each has its own drawbacks.
  • Having aggregate validation fail multiple fields is difficult - the only way you can add intelligible error messages is via binding the callback to the validator instance, putting other fields out of reach.

Proposal

Implement a way to perform aggregate validation, after all fields have been filtered and validated.

Whether aggregate validation occurs will be dependent on whether the user wants all fields to first be individually valid or not.

Aggregate validation will only be usable via a public method on InputFilter such as attachAggregateValidator(callable $validator, bool $requireValidPayload) where the callable will be well defined.

It must also be easy for the user to indicate:

  • which fields should be considered as failed
  • add descriptive error messages to each failing field
  • ensure that error messages are appended to any existing errors for the relevant fields.

I don't envisage adding this to the array-based factory spec for input filters.

Metadata

Metadata

Assignees

No one assigned

    Projects

    Status

    Todo

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions