String Rules
String validation rules check the content and format of string values. All rules are Unicode-aware.
Rules Reference
Section titled “Rules Reference”| Rule | Parameters | Example | Error Message |
|---|---|---|---|
alpha | — | $v->rule('alpha', 'field') | {field} must contain only letters |
alphaNum | — | $v->rule('alphaNum', 'field') | {field} must contain only letters and numbers |
ascii | — | $v->rule('ascii', 'field') | {field} must contain only ASCII characters |
slug | — | $v->rule('slug', 'field') | {field} must contain only letters, numbers, dashes and underscores |
contains | substring, strict? | $v->rule('contains', 'field', 'text') | {field} must contain {0} |
regex | pattern | $v->rule('regex', 'field', '/pattern/') | {field} is not valid |
startsWith | prefix, caseSensitive? | $v->rule('startsWith', 'field', 'pre') | {field} must start with {0} |
endsWith | suffix, caseSensitive? | $v->rule('endsWith', 'field', 'suf') | {field} must end with {0} |
uuid | version? | $v->rule('uuid', 'field') | {field} must be a valid UUID |
passwordStrength | minScore or config[] | $v->rule('passwordStrength', 'field', 6) | {field} must be a stronger password |
Note: Parameters marked with
?are optional.
Unicode support: alpha, alphaNum accept Unicode letters. slug only allows a-z, 0-9, -, _.
Case sensitivity: contains, startsWith, endsWith are case-insensitive by default. Pass true as last param for strict matching.
Multiple values: startsWith, endsWith accept arrays: ['http://', 'https://'].
Regex security: Includes ReDoS protection - dangerous patterns throw RuntimeException.
UUID versions: 1 (time), 2 (DCE), 3 (MD5), 4 (random), 5 (SHA-1). Pass version as 2nd param or omit for any.
Parameter Details
Section titled “Parameter Details”passwordStrength
Section titled “passwordStrength”Accepts integer (minimum score 1-10) or configuration array:
| Option | Type | Default | Description |
|---|---|---|---|
minScore | int | 6 | Minimum strength score (1-10) |
minLength | int | 8 | Minimum password length |
requireUppercase | bool | false | Must contain A-Z |
requireLowercase | bool | false | Must contain a-z |
requireNumber | bool | false | Must contain 0-9 |
requireSymbol | bool | false | Must contain special char |
Scoring: +1 per length tier (8+, 12+, 16+), +1.5 upper, +1.5 lower, +2 number, +2 symbol. Max 10.
// Simple$v->rule('passwordStrength', 'password', 8);
// Full config$v->rule('passwordStrength', 'password', [ 'minScore' => 7, 'requireUppercase' => true, 'requireNumber' => true]);Complete 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([ 'alpha' => [ ['first_name'], ['last_name'] ], 'alphaNum' => [ ['username'] ], 'ascii' => [ ['product_code'] ], 'slug' => [ ['url_slug'] ], 'contains' => [ ['bio', 'developer'] ], 'regex' => [ ['sku', '/^[A-Z]{2}-\\d{4}$/'] ], 'startsWith' => [ ['website', 'https://'] ], 'endsWith' => [ ['work_email', '@company.com'] ], 'uuid' => [ ['user_id'] ], 'passwordStrength' => [ ['password', 6] ]]);
$v->validate();Password Strength with Full Configuration
Section titled “Password Strength with Full Configuration”$v->rules([ 'passwordStrength' => [ ['password', [ 'minScore' => 7, 'minLength' => 10, 'requireUppercase' => true, 'requireLowercase' => true, 'requireNumber' => true, 'requireSymbol' => true ]] ]]);Field-Based Mapping
Section titled “Field-Based Mapping”use Frostybee\Valicomb\Validator;
$v = new Validator($data);
$v->mapManyFieldsToRules([ 'username' => ['required', 'alphaNum', ['lengthBetween', 3, 20]], 'first_name' => ['required', 'alpha'], 'last_name' => ['required', 'alpha'], 'bio' => ['optional', ['contains', 'developer']], 'password' => ['required', ['passwordStrength', 7]], 'website' => ['optional', ['startsWith', 'https://']], 'work_email' => ['required', 'email', ['endsWith', '@company.com']]]);
$v->validate();