Filters reference¶
All filters are in the Verja\Filter\ namespace. In string definitions, use the class
name with the first letter lowercase: 'trim', 'integer', 'convertCase:upper'.
Short syntax with IDE autocompletion¶
Just like validators, filters support a short static syntax via __callStatic:
use Verja\Filter as f;
$gate->accepts([
'username' => [f::trim(), f::convertCase('lower')],
'price' => [f::numeric()],
]);
For custom filters, extend and annotate:
Filters run before validators and transform the value. If a filter cannot process the
value (wrong type, invalid format), it throws Verja\Exception\InvalidValue, which
short-circuits the pipeline and adds an error — no further filters or validators run.
String filters¶
Trim¶
'trim', 'trim:characters', f::trim($characterMask = " \t\n\r\0\x0B"),
new Filter\Trim($characterMask = " \t\n\r\0\x0B")
Trims $characterMask from both ends of the string. Defaults to PHP's standard
whitespace characters.
ConvertCase¶
Converts the string case using mb_convert_case().
Non-string values pass through unchanged. $mode must be one of:
- 'upper' — converts to uppercase
- 'lower' — converts to lowercase
- 'title' — converts to title case
Escape¶
Escapes special HTML characters using htmlspecialchars(). Useful when values are
rendered directly into HTML.
Replace¶
Replaces all occurrences of $search with $replace in the string.
PregReplace¶
'pregReplace:pattern:replacement', f::pregReplace($pattern, $replace),
new Filter\PregReplace($pattern, $replace)
Replaces matches of the PCRE pattern $pattern with $replace. $replace can be a
replacement string or a callable (maps to preg_replace_callback).
StripTags¶
'stripTags', 'stripTags:<p><strong>', f::stripTags($allowedTags = null),
new Filter\StripTags($allowedTags = null)
Strips HTML tags from strings using strip_tags(). Pass allowed tags to preserve specific
elements. Non-string values pass through unchanged.
Type-casting filters¶
These filters validate the input type before casting and throw InvalidValue if the
value cannot be converted. This makes them safe to combine with validators that expect
a specific type.
Integer¶
Converts the value to an integer. Accepts integer values and numeric strings. Throws if
the value is not numeric. After this filter, downstream validators always see a PHP int.
->int('age', 'between:0:150')
// 'int()' applies Filter\Integer, then Between checks the integer value
Numeric¶
Converts the value to a float or integer (integer if no decimal part). Accepts numeric
strings. Pass $decimalPoint to support alternative decimal separators (e.g. ',' for
European locales). Throws if the value is not numeric.
Boolean¶
'boolean', f::boolean(),
new Filter\Boolean($stringTrue = [], $stringFalse = [], $overwrite = false)
Converts the value to a PHP boolean. Accepts bool, int, and the following strings by
default:
- truthy:
'1','true','t','yes','y' - falsy:
'0','false','f','no','n'
Pass $stringTrue / $stringFalse to add extra accepted representations. Pass
$overwrite = true to replace the defaults entirely. Throws InvalidValue for any other
value.
DateTime¶
'dateTime', f::dateTime($timeZone = null, $format = null, $strict = false),
new Filter\DateTime($timeZone = null, $format = null, $strict = false)
Converts a string to a PHP \DateTime object. Without $format, parses any string
accepted by PHP's DateTime constructor. With $format, requires the value to match
that format (delegates to DateTime::createFromFormat()). $strict = true additionally
rejects overflow dates (e.g. 2025-01-32). $timeZone can be a \DateTimeZone instance,
a timezone name string, or a UTC offset integer.
Throws InvalidValue if the value does not parse.
->date('published_at')
// '2024-03-15' → DateTime object (no format — accepts any parseable date)
f::dateTime(null, 'Y-m-d')
// '2024-03-15' → DateTime, '15.03.2024' → InvalidValue
Array filters¶
ArrayFilter¶
Removes elements from arrays. Non-array values pass through unchanged. By default removes
null and empty string ''. Pass a callback to define custom keep logic:
f::arrayFilter() // remove null and ''
f::arrayFilter(fn($v) => $v !== null) // remove only null
f::arrayFilter(fn($v) => is_int($v)) // keep only integers
Split¶
Splits a string into an array by $delimiter. Trims whitespace from each element by
default. Non-string values pass through unchanged.
f::split(',') // 'a, b , c' → ['a', 'b', 'c']
f::split(',', false) // 'a, b , c' → ['a', ' b ', ' c']
Utility filters¶
DefaultValue¶
Returns $default when the value is null, passes everything else through unchanged.
Useful after Nullable to provide a fallback:
Other¶
Callback¶
Delegates to a custom callable. The callable receives ($value, $context) and returns
the transformed value. Cannot be used as a string definition. See
custom validators and filters.