Back to Validation

EndsWith

docs/validators/EndsWith.md

3.1.12.7 KB
Original Source
<!-- SPDX-License-Identifier: MIT SPDX-FileCopyrightText: (c) Respect Project Contributors SPDX-FileContributor: Alexandre Gomes Gaigalas <[email protected]> SPDX-FileContributor: Henrique Moody <[email protected]> -->

EndsWith

  • EndsWith(mixed $endValue)
  • EndsWith(mixed $endValue, mixed ...$endValues)

This validator is similar to Contains(), but validates only if one of the values is at the end of the input. Only string inputs and string end values are checked; non‑string values are considered invalid but will not produce PHP errors thanks to internal type guards.

For strings (non-string inputs are always rejected):

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

v::endsWith(', PhD', ', doctor')->assert('Jane Doe, PhD');
// Validation passes successfully

For arrays:

php
v::endsWith('ipsum')->assert(['lorem', 'ipsum']);
// Validation passes successfully

v::endsWith('.', ';')->assert(['this', 'is', 'a', 'tokenized', 'phrase', '.']);
// Validation passes successfully

v::endsWith('.', ';')->assert(['this', 'is', 'a', 'tokenized', 'phrase']);
// → `["this", "is", "a", "tokenized", "phrase"]` must end with "." or ";"

Message template for this validator includes {{endValue}} and {{endValues}}.

Templates

EndsWith::TEMPLATE_STANDARD

ModeTemplate
default{{subject}} must end with {{endValue}}
inverted{{subject}} must not end with {{endValue}}

EndsWith::TEMPLATE_MULTIPLE_VALUES

ModeTemplate
default{{subject}} must end with {{endValues|list:or}}
inverted{{subject}} must not end with {{endValues|list:or}}

Template placeholders

PlaceholderDescription
subjectThe validated input or the custom validator name (if specified).
endValueThe value that will be checked to be at the end of the input.
endValuesAdditional values to check.

Categorization

  • Arrays
  • Strings

Changelog

VersionDescription
3.1.0Added support for multiple values
3.0.0Case-insensitive comparison removed
0.3.9Created

See Also