Skip to content

v1.1.0

Latest

Choose a tag to compare

@SoloPHP SoloPHP released this 28 Oct 14:08
002c374

One meaning for an empty input: null, and a "" the field's type cannot hold, are no value. The factory reads such a "" as null before validation, the validator checks a null like a missing field — by presence rules alone — and the field holds null, keeps its default or reports required.

#[Validate('string|max:100')]
#[EmptyToNull]
public ?string $q = null,      // ?q=    → null (1.0: `string` error)

#[Validate('boolean')]
public ?bool $done = null,     // ?done= → null (1.0: `boolean` error)

#[Validate('integer|min_value:1')]
public int $page = 1,          // ?page= → 1    (1.0: `integer` error)

Fixes

  • #[EmptyToNull] gives what it documents for a field with rules: a blank string is null, the default or required, where it failed the field's first rule — string for a string the client sent.
  • A rule matching the type is harmless for no value, as documented: ?done= for a boolean ?bool is null.

Behavior changes

  • A nullable field takes null without nullable in its rules, so in a partial update a null clears it. Keep it from being cleared with filled: #[Validate('filled|string|max:255')] public ?string $name = null.
  • A field with a default keeps it for no value — in a partial update a null resets it; one with neither reports required instead of the first rule it failed.
  • A "" for an array or #[Items] field, or for a custom caster's array type, is no value instead of []. Only a [] the client sends is the empty list.
  • Rules of your own run on no value only as presence rules: #[Validate('accepted')] public bool $terms = false takes false for null — drop the default and write required|accepted.
  • A null in #[FromContext] is no value too; rules that read another field see null where they saw "".
  • Rule strings reach the validator verbatim; a nullable you write keeps working.

Requirements

  • solophp/contracts ^1.8, whose ValidatorInterface checks a missing or null field by presence rules alone and conflicts with solophp/validator below 4.2 — so solophp/validator 4.2 or a validator of your own that keeps the rule.

See the upgrade guide for a 1.0 / 1.1 table of each change, and the full diff.