respect-validation/docs/validators/Formatted.md
Henrique Moody 6abf3a548c
Remove Masked validator in favor of Formatted
The Masked validator was a proxy for what the new Formatted validator
already does, so it is being removed to reduce redundancy. All tests and
documentation have been updated accordingly.

Assisted-by: OpenCode (ollama-cloud/glm-4.7)
2026-02-06 20:44:26 +01:00

1.8 KiB

Formatted

  • Formatted(Formatter $formatter, Validator $validator)

Decorates a validator to format input values in error messages while still validating the original input.

use Respect\StringFormatter\FormatterBuilder as f;

v::formatted(f::mask('1-4'), v::email())->assert('foo@example.com');
// Validation passes successfully

v::formatted(f::mask('1-4'), v::email())->assert('not an email');
// → "****an email" must be an email address

v::formatted(f::pattern('#### #### #### ####'), v::creditCard())->assert('4111111111111111');
// Validation passes successfully

v::formatted(f::pattern('#### #### #### ####'), v::creditCard())->assert('1234123412341234');
// → "1234 1234 1234 1234" must be a credit card number

This validator is useful for displaying formatted values in error messages, making them more readable for end users. For example, showing credit card numbers with spaces or phone numbers with proper formatting.

It uses respect/string-formatter as the underlying formatting engine. See the StringFormatter documentation for available formatters.

Behavior

The validator first ensures the input is a valid string using StringVal. If the input passes string validation, it validates the original input using the inner validator. The formatted version is only used for display in error messages.

Categorization

  • Display
  • Transformations

Changelog

Version Description
3.0.0 Created

See Also