respect-validation/docs/validators/Trimmed.md
Alexandre Gomes Gaigalas 2600db3c74 Introduce Trimmed validator
This commit introduces the `Trimmed` validator that ensures a string
cannot start or end with a list of specific values.

The default values used are a selected list of Unicode invisible
characters.

To support this change, the StartsWith and EndsWith validators were
modified so they can also support multiple values to check for.

While StartsWith and EndsWith are more generic, and also perform
start-of-array and end-of-array kinds of checks, Trimmed is more
focused on string inputs, which tailors to a more specific use
case.
2026-02-26 10:34:53 +00:00

2.4 KiB
Raw Permalink Blame History

Trimmed

  • Trimmed()
  • Trimmed(string ...$trimValues)

Validates whether the input string does not start or end with the given values.

When no values are provided, this validator uses a default list of Unicode invisible characters (including regular whitespace, non-breaking spaces, and zero-width characters).

With the default values:

v::trimmed()->assert('lorem ipsum');
// Validation passes successfully

v::trimmed()->assert("\u{200B}lorem");
// → "lorem" must not contain leading or trailing whitespace

With custom values:

v::trimmed('Dr.', 'Mr.', 'PhD.')->assert('John');
// Validation passes successfully

v::trimmed('Dr.', 'Mr.', 'PhD.')->assert('Dr. John');
// → "Dr. John" must not contain leading or trailing "Dr.", "Mr.", or "PhD."

v::trimmed('Dr.', 'Mr.', ', PhD')->assert('John Doe, PhD');
// → "John Doe, PhD" must not contain leading or trailing "Dr.", "Mr.", or ", PhD"

This validator composes StartsWith and EndsWith.

Templates

Trimmed::TEMPLATE_STANDARD

Mode Template
default {{subject}} must not contain leading or trailing whitespace
inverted {{subject}} must contain leading or trailing whitespace

Trimmed::TEMPLATE_CUSTOM

Mode Template
default {{subject}} must not contain leading or trailing {{trimValues|list:or}}
inverted {{subject}} must contain leading or trailing {{trimValues|list:or}}

Template placeholders

Placeholder Description
subject The validated input or the custom validator name (if specified).
trimValues The values that will be checked at start end end of input.

Categorization

  • Strings

Changelog

Version Description
3.1.0 Created

See Also