1. Go to this page and download the library: Download affinity4/validate 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/ */
affinity4 / validate example snippets
use Affinity4\Validate\Validate;
class ClassWithValidation
{
use Validate;
/**
* @validation type(int:unsigned)
*/
protected $id;
/**
* @validation type(string:alphanumeric)
*/
protected $username;
}
$Validate = new ClassWithValidation;
$Validate->id = null
$Validate->username = 'not_alphanumeric123';
if (!$Validate->isValid()) {
/*
The validation errors are grouped by property...
$validations_errors = [
'id' => ValidationError([
[
"class" => "\ClassWithValidation",
"property" => "id"
"value" => null,
"error" => "Must be of type string"
]
]),
'username => ValidationError([
[
"class" => "\ClassWithValidation",
"property" => "username",
'value' => "not_alphanumeric123",
'error' => "Must be alphanumeric (letters and numbers only)"
]
])
]
*/
foreach ($Validate->getValidationErrors() as $ValidationErrors) {
/*
NOTE:
$ValidationErrors is an instance of \Affinity4\Validate\ValidationErrors
See section ValidationErrors object
*/
}
}
@validation type(string:alnum)
// or @validation type(string:alphanum)
// or @validation type(string:alphanumeric)
@validation type(string:snakecase)
@validation type(string:kebabcase)
@validation type(string:constantcase)
// or @validation type(string:uppersnakecase)
@validation type(string:camelcase)
@validation type(string:pascalcase)
// or @validation type(string:camelcaps)
// or @validation type(string:studlycaps)
// or @validation type(string:capitalcase)
@validation type(string:traincase)
@validation type(int:unsigned)
/*
* @validation type(int:unsigned|cast)
*/
protected $id;
/*
* @validation type(string:alnum|cast)
*/
protected $username;
// ...
class User {
// ...
public function __toString()
{
return 'user' . $this->user_id;
}
}
$Class->username = new User; // "user001";
/**
* Credit Card Number
*
* Matches an a credit card number (e.g. 1234 1234 1234) and encrypts it (e.g **** **** 1234):
*
* @validation replace((\d{4})\s*(\d{4})\s*(\d{4}), **** **** ${3})
*
* @var string
*/
protected $credit_card_number;
[
"password" => [
[
"class" => "\Acme\ClassWithValidation",
"property" => "password",
"value" => "password",
"error" => "Password length must be greater than 8 characters"
],
[
"class" => "\Acme\ClassWithValidation",
"property" => "password",
"value" => "password",
"error" => "Password must have at least one capital letter"
],
[
"class" => "\Acme\ClassWithValidation",
"property" => "password",
"value" => "password",
"error" => "Password must have at least one number"
]
]
]
$errors['password'][0]['error'] // Password length must be greater than 8 characters
$errors['password'][3]['error'] // Ooops: There's no index 3, but a very easy and common mistake to make
// Or what if you just wanted all the password errors?
$password_errors = []
foreach ($errors['password'] as $error)
{
$password_errors[] = $error['error'];
}
$password_errors = $Validate->getValidationErrors('password')->errors();
/*
$password_errors = [
"Password length must be greater than 8 characters",
"Password must have at least one capital letter",
"Password must have at least one number"
]
*/
$Stub->getValidationErrors('password')->count(); // 3
$PasswordValidationErrors = $Stub->getValidationErrors('password');
$PasswordValidationErrors->first()->error; // Password length must be greater than 8 characters
$PasswordValidationErrors->next()->error; // Password must have at least one capital letter
$PasswordValidationErrors->prev()->error; // Password length must be greater than 8 characters
$PasswordValidationErrors->last()->error; // Password must have at least one number
// We also can get class, property and value
$PasswordValidationErrors->first()->class; // \Acme\Validate
$PasswordValidationErrors->first()->property; // 'password'
$PasswordValidationErrors->first()->value; // 'password'
@validation type(string:cast|kebabcase).to(snakecase) // Fails if not a string formatted as kebabcase. Otherwise converts kebabcase to snakecase (e.g. "i-am-a-kebab" to "i_am_a_kebab")
// or
@validation match(\s+).to(lowercase|snakecase) // Fails if not a sentence (no spaces found). Otherwise, converts sentences to lowercase snakecase (e.g. "I Am A Title" to "i_am_a_title")
// or
@validation match(\d{4}\s*\s{4}\s*\d{4}).replace((\d{4})\s*(\d{4})\s*(\d{4}), **** **** ${3}) // Fails if not a credit card number. Otherwise, encrypts it (e.g. **** **** 1234)
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.