About Validation
- A PHP library for data validations
- Requires PHP Extension FINFO.
Composer Install
composer require phpbook/validation
Declare Configurations
Declare Validation
layout = new \PHPBook\Validation\Layout;
$this->layout->setAttribute('id', [
'label' => 'Id',
'type' => '@string',
'uuid' => true,
'required' => true,
])
->setAttribute('age', [
'label' => 'Age',
'type' => '@integer',
'required' => true,
'min' => 18,
'max' => 100
])
->setAttribute('document', [
'label' => 'Document',
'type' => '@digits',
'required' => true,
'maxlength' => 15
])
->setAttribute('weight', [
'label' => 'Weight',
'type' => '@float',
'required' => false
])
->setAttribute('name', [
'label' => 'Name',
'type' => '@string',
'required' => true,
'minlength' => 5,
'maxlength' => 120
])
->setAttribute('photo', [
'label' => 'Photo',
'type' => '@file-buffer',
'required' => true,
'maxkbs' => 100,
'mimes' => ['image' => 'Image File']
])
->setAttribute('status', [
'label' => 'Status',
'type' => '@string',
'required' => false,
'options' => [static::$STATUS_ACTIVE => 'Active', static::$STATUS_INACTIVE => 'Inactive', static::$STATUS_PROCESS => 'Process']
])
->setAttribute('friend', [
'label' => 'Friend',
'type' => '\App\Module\Friend\Entity',
'hints' => 'Friends',
'required' => false
])
->setAttribute('friends', [
'label' => 'Friends',
'type' => '@array:\App\Module\Friend\Entity',
'hints' => 'List of Friends'
])
->setAttribute('numbers', [
'label' => 'Numbers',
'type' => '@array:@integer',
])
->setAttribute('active', [
'label' => 'Ativo',
'type' => '@boolean',
'required' => true
])
->setAttribute('date', [
'label' => 'Date',
'type' => '@date',
'required' => true,
])
->setAttribute('datetime', [
'label' => 'Date Time',
'type' => '@datetime',
'required' => true
])
->setAttribute('time', [
'label' => 'Time',
'type' => '@time',
'required' => true
])
->setRule('ageOfJhon', ['name', 'age'], function($name, $age) {
/* rules validations are called after the attributes validation */
if (($name) and ($name == 'jhon')) {
if (($age) and ($age setRule('anaCantBeHere', ['name'], function($name) {
/* rules validations are called after the attributes validation */
if ($name == 'ana') {
/* you should throw exception like the basic validation does */
throw new Exception('What are you doing here Ana?');
};
})
->setRule('statuRequiredForJhon', ['name', 'status'], function($name, $status) {
/* rules validations are called after the attributes validation */
if ($name == 'jhon') {
if (!$status) {
/* you should throw exception like the basic validation does */
throw new Exception('Status is required for Jhon');
};
};
});
}
public function getLayout(): \PHPBook\Validation\Layout {
return $this->layout;
}
}
?>
Use Validation
id, $this->name, $this->age) = $customerValidation->getLayout()->validate([null, $name, $age], ['id', 'name', 'age']);
} catch(\Exception $e) {
echo $e->getMessage();
};
}
public function getName() {
return $this->name;
}
public function getAge() {
return $this->age;
}
}
/* You can iterate the attributes */
$customerValidation = new CustomerValidation;
$attributes = $customerValidation->getLayout()->getAttributes();
foreach($attributes as $name => $attribute) {
$name; //age
$attribute; //['type' => '@integer', 'required' => true]
};
/* You can iterate and verbose the attributes */
$customerValidation = new CustomerValidation;
$attributes = $customerValidation->getLayout()->getAttributes();
foreach($attributes as $name => $attribute) {
$customerValidation->getLayout()->getAttributeVerbose($name); //name must be a string. name must have max 120 characters. name is required
};
/* You can iterate the rules */
$customerValidation = new CustomerValidation;
$rules = $customerValidation->getLayout()->getRules();
foreach($rules as $key => $rule) {
list($name, $attributes, $closure) = $rule;
};
/* You can get the rules verbose */
$customerValidation->getLayout()->getRulesVerbose(); //one string with all the rules descriptions
?>
- Validation of date, datetime and time follows the international string format YYYY-MM-DD H:i:s | YYYY-MM-DD | H:i:s
- Validation of date, datetime and time enable using DateTime class as value to be validated