1. Go to this page and download the library: Download laraantunes/validare 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/ */
laraantunes / validare example snippets
class MyClass {
use \Validare\Bind;
public $myField;
/**
* Validare unction rules() {
$this->validate('myField', \Validare\Rule::REQUIRED);
}
}
$myObj = new MyClass();
echo $myObj->valid(); // It'll return false
// It'll return an array with all the validation errors found
var_dump($myObj->validationErrors());
// Adds a REQUIRED validation for myField:
$myObj->validate('myField', \Validare\Rule::REQUIRED);
// You may pass more than one rule when calling validate()!
$myObj->validate('myField', [\Validare\Rule::LESS_OR_EQUALS => 5], [\Validare\Rule::MORE => 3]);
// When passing a value that must be compared, pass the rule as an array:
$myObj->validate('myField', [\Validare\Rule::LENGTH => 4]);
// You need to use the full array syntax if you need to pass a custom rule name for errors:
$myObj->validate('myField', [
'rule' => \Validare\Rule::MAX_LENGTH,
'compare' => 5,
'ruleName' => 'My Rule Name',
]);
// You may also pass closures as rules!
$myObj->validate('myField', function($value){
return $value->value == 5;
});
// And even with 'use' clause:
$myObj->validate('myField', function($value) use ($customValue){
return $value->value == $customValue;
});
// And if you need to handle all the \Validare\Rule object, you may pass a new object:
$myObj->validate('myField', new \Validare\Rule(1, \Validare\Rule::EQUALS, 1));
// if you need to use a callback after validate, use a \Validate\Rule object:
$myObj->validate(
'myField',
new \Validare\Rule(
$value,
\Validare\Rule::IS_STRING,
null,
null,
function($success){
if ($success) {
// Do something!
}
}
)
);
// Magic Calls for default rules:
\Validare\Assert::
// Default assert (rules may be called as the same as \Validare\Bind::validate() method):
\Validare\Assert::value('Valid', \Validare\Rule::REQUIRED); // Returns true
\Validare\Assert::value(
2,
[\Validare\Rule::LESS_OR_EQUALS => 5],
[\Validare\Rule::MORE => 3]
); // Returns false
// If you need to check if an object has an attribute defined, use 'has_attribute' rule:
\Validare\Assert::has_attribute($obj, 'test');
// And if you need to check a value of an object, use 'has_attribute_value' rule, with an
// array on compareValue:
\Validare\Assert::has_attribute_value($obj, ['test' => 'wololo']);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.