PHP code example of codesvault / validator
1. Go to this page and download the library: Download codesvault/validator library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
codesvault / validator example snippets
$validator = Validator::validate(
[
'username' => 'uired|min:8',
'email' => '
$validator = Validator::validate(
[
'username' => '' => 'abmsourav',
'full_name' => 'Keramot UL Islam'
]
);
$validator = Validator::validate(
['username' => 'nvalid examples:
// ['username' => ''] // ❌ Empty string
// ['username' => null] // ❌ Null value
// [] // ❌ Missing field
$validator = Validator::validate(
['email' => 'email'],
['email' => '[email protected] '] // ✅ Valid
);
// Invalid examples:
// ['email' => 'invalid-email'] // ❌ Invalid format
// ['email' => '@example.com'] // ❌ Missing username
// ['email' => 'user@'] // ❌ Missing domain
$validator = Validator::validate(
['website' => 'url'],
['website' => 'https://example.com'] // ✅ Valid
);
// Also valid:
// ['website' => 'http://example.com']
// ['website' => 'ftp://files.example.com']
// Invalid examples:
// ['website' => 'not-a-url'] // ❌ Invalid format
// ['website' => 'example.com'] // ❌ Missing protocol
$validator = Validator::validate(
['name' => 'stringOnly'],
['name' => 'John'] // ✅ Valid
);
// Invalid examples:
// ['name' => 'John123'] // ❌ Contains numbers
// ['name' => 'John Doe'] // ❌ Contains space
// ['name' => 'John-Doe'] // ❌ Contains dash
$validator = Validator::validate(
['full_name' => 'stringWithSpace'],
['full_name' => 'John Doe'] // ✅ Valid
);
// Also valid:
// ['full_name' => 'Mary Jane Watson']
// Invalid examples:
// ['full_name' => 'John123'] // ❌ Contains numbers
// ['full_name' => 'John-Doe'] // ❌ Contains dash
$validator = Validator::validate(
['username' => 'stringWithNumber'],
['username' => 'user123'] // ✅ Valid
);
// Also valid:
// ['username' => 'JohnDoe456']
// Invalid examples:
// ['username' => 'user 123'] // ❌ Contains space
// ['username' => 'user-123'] // ❌ Contains dash
$validator = Validator::validate(
['slug' => 'stringWithDash'],
['slug' => 'hello-world_page'] // ✅ Valid
);
// Also valid:
// ['slug' => 'my_awesome_post']
// ['slug' => 'hello-world']
// Invalid examples:
// ['slug' => 'hello world'] // ❌ Contains space
// ['slug' => 'hello123'] // ❌ Contains numbers
// Uppercase only
$validator = Validator::validate(
['code' => 'uppercase'],
['code' => 'HELLO'] // ✅ Valid
);
// Lowercase only
$validator = Validator::validate(
['tag' => 'lowercase'],
['tag' => 'hello'] // ✅ Valid
);
// Mixed case (both upper and lower case
$validator = Validator::validate(
['password' => 'stringWithSymbols'],
['password' => 'Pass@123!'] // ✅ Valid
);
// Also valid:
// ['password' => 'My$ecur3#Pass']
// String minimum length
$validator = Validator::validate(
['password' => 'min:8'],
['password' => 'mypassword'] // ✅ Valid (10 characters)
);
// Number minimum value
$validator = Validator::validate(
['age' => 'min:18'],
['age' => 25] // ✅ Valid
);
// Array minimum items
$validator = Validator::validate(
['tags' => 'min:2'],
['tags' => ['php', 'javascript', 'html']] // ✅ Valid (3 items)
);
// String maximum length
$validator = Validator::validate(
['title' => 'max:100'],
['title' => 'Short Title'] // ✅ Valid
);
// Number maximum value
$validator = Validator::validate(
['score' => 'max:100'],
['score' => 85] // ✅ Valid
);
// Array maximum items
$validator = Validator::validate(
['categories' => 'max:5'],
['categories' => ['tech', 'news']] // ✅ Valid (2 items)
);
$validator = Validator::validate(
['age' => 'integer'],
['age' => 25] // ✅ Valid
);
// Also valid:
// ['age' => '30'] // ✅ String numbers are valid
// Invalid examples:
// ['age' => 25.5] // ❌ Float value
// ['age' => 'twenty'] // ❌ Text value
$validator = Validator::validate(
['tags' => 'array'],
['tags' => ['php', 'javascript']] // ✅ Valid
);
// Invalid examples:
// ['tags' => 'php,javascript'] // ❌ String value
// ['tags' => 123] // ❌ Number value
$validator = Validator::validate(
['confirmation' => 'sameValue:yes'],
['confirmation' => 'yes'] // ✅ Valid
);
// For password confirmation:
$validator = Validator::validate(
[
'password' => '
// Validate each email in an array
$validator = Validator::validate(
['emails' => 'each:email'],
['emails' => ['[email protected] ', '[email protected] ']] // ✅ Valid
);
// Validate each item with multiple rules (`&` [AND] operator)
$validator = Validator::validate(
['usernames' => 'each:
$validator = Validator::validate(
[
'username' => 'equired|min:8|mixedCase',
'age' => 'username' => 'johndoe',
'email' => '[email protected] ',
'password' => 'MySecure123',
'age' => 25,
'tags' => ['php', 'javascript'],
'skills' => ['programming', 'design']
]
);
$data = $validator->getData();
$validator = Validator::validate(
[
'username' => 'name' => 'ab', // Too short
'email' => 'invalid-email' // Invalid format
]
);
$errors = $validator->error();
if ($errors) {
// Handle validation errors
foreach ($errors as $field => $message) {
echo "Error in {$field}: {$message[0]}\n";
}
}
$data = $validator->getData();