Skip to content

Length Rules

Length validation rules check string length. All rules use multibyte-aware character counting (UTF-8 support).

RuleParametersExampleError Message
lengthlength$v->rule('length', 'field', 4){field} must be exactly {0} characters long
lengthBetweenmin, max$v->rule('lengthBetween', 'field', 3, 20){field} must be between {0} and {1} characters long
lengthMinmin$v->rule('lengthMin', 'field', 8){field} must be at least {0} characters long
lengthMaxmax$v->rule('lengthMax', 'field', 255){field} must be at most {0} characters long

All length rules use mb_strlen() for accurate Unicode character counting:

// All count as 5 characters:
'hello' // 5 ASCII chars
'héllo' // 5 chars (é = 1 char)
'こんにちは' // 5 Japanese chars

Valicomb supports two syntax styles: rule-based (map rules to fields) and field-based (map fields to rules).

use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->rules([
'length' => [
['pin_code', 4],
['country_code', 2]
],
'lengthBetween' => [
['username', 3, 20],
['display_name', 2, 50]
],
'lengthMin' => [
['password', 8],
['description', 10]
],
'lengthMax' => [
['username', 30],
['bio', 500],
['tweet', 280]
]
]);
$v->validate();
use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->mapManyFieldsToRules([
'username' => ['required', ['lengthBetween', 3, 20]],
'password' => ['required', ['lengthMin', 8], ['lengthMax', 128]],
'pin_code' => ['required', ['length', 4]],
'bio' => ['optional', ['lengthMax', 500]],
'title' => ['required', ['lengthBetween', 5, 100]],
'country_code' => ['required', ['length', 2]]
]);
$v->validate();