Length Rules
Length validation rules check string length. All rules use multibyte-aware character counting (UTF-8 support).
Rules Reference
Section titled “Rules Reference”| Rule | Parameters | Example | Error Message |
|---|---|---|---|
length | length | $v->rule('length', 'field', 4) | {field} must be exactly {0} characters long |
lengthBetween | min, max | $v->rule('lengthBetween', 'field', 3, 20) | {field} must be between {0} and {1} characters long |
lengthMin | min | $v->rule('lengthMin', 'field', 8) | {field} must be at least {0} characters long |
lengthMax | max | $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 charsComplete Examples
Section titled “Complete Examples”Valicomb supports two syntax styles: rule-based (map rules to fields) and field-based (map fields to rules).
Rule-Based Array Syntax
Section titled “Rule-Based Array Syntax”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();Field-Based Mapping
Section titled “Field-Based Mapping”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();