Skip to content

Quick Start

This guide will get you up and running with Valicomb in just a few minutes.

The core workflow is simple: create a Validator with your data, add rules for each field, and call validate(). If validation fails, you can retrieve the error messages.

use Frostybee\Valicomb\Validator;
// Create validator with data
$v = new Validator(['email' => 'test@example.com', 'age' => '25']);
// Add validation rules
$v->rule('required', ['email', 'age']);
$v->rule('email', 'email');
$v->rule('integer', 'age');
// Run validation
if ($v->validate()) {
echo "Validation passed!";
} else {
print_r($v->errors());
}

HTML forms submit all values as strings, even numbers and booleans. Valicomb’s type validation rules (like integer, numeric, and boolean) are designed to work with string representations, so you can validate $_POST data directly without manual type casting.

Valicomb properly handles form data where all values come as strings:

// $_POST data - everything is strings
$_POST = [
'age' => '25', // String, not int
'price' => '19.99', // String, not float
'active' => '1' // String, not bool
];
$v = new Validator($_POST);
$v->rule('integer', 'age'); // Works with string '25'
$v->rule('numeric', 'price'); // Works with string '19.99'
$v->rule('boolean', 'active'); // Works with string '1'

Each validation rule has a default error message, but you can replace it with your own using the message() method. This lets you tailor messages to your application’s tone or provide more specific guidance.

Override default error messages for specific rules:

$v = new Validator($_POST);
$v->rule('required', 'email')->message('We need your email!');
$v->rule('email', 'email')->message("That doesn't look like a valid email");

By default, error messages use the field name (like “email” or “first_name”). Labels let you define human-readable names that appear in error messages instead, making them clearer for your users.

Make error messages more user-friendly with labels:

$v = new Validator($_POST);
$v->labels([
'email' => 'Email Address',
'password' => 'Password'
]);

When validate() returns false, you can retrieve error messages using the errors() method. Call it without arguments to get all errors grouped by field, or pass a field name to get errors for just that field.

After validation fails, retrieve errors:

$v = new Validator($_POST);
$v->rule('required', 'email');
$v->rule('integer', 'age');
if (!$v->validate()) {
// Get all errors
$errors = $v->errors();
// ['email' => ['Email is required'], 'age' => ['Age must be an integer']]
// Get errors for specific field
$emailErrors = $v->errors('email');
// ['Email is required']
}

Now that you know the basics, explore these guides to learn more about Valicomb’s features.