Quick Start
This guide will get you up and running with Valicomb in just a few minutes.
Basic Validation
Section titled “Basic Validation”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 validationif ($v->validate()) { echo "Validation passed!";} else { print_r($v->errors());}Handling Form Data
Section titled “Handling Form Data”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'Custom Error Messages
Section titled “Custom Error Messages”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");Field Labels
Section titled “Field Labels”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']);Getting Errors
Section titled “Getting Errors”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']}Next Steps
Section titled “Next Steps”Now that you know the basics, explore these guides to learn more about Valicomb’s features.
- Core Concepts - Understand the validation workflow
- Defining Rules - Master all three rule syntaxes
- Validation Rules - Explore all 53 built-in rules
- Error Messages - Customize error handling