1. Go to this page and download the library: Download dcp/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.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
dcp / validator example snippets
useDCP\Form\Validation\Validator;
$validator = new Validator();
useDCP\Form\Validation\Validator;
useDCP\Form\Validation\RuleSet;
useDCP\Form\Validation\Rule;
useDCP\Form\Validation\Constraint;
$ruleSet = (new RuleSet())
->add(
(new Rule())
->setFieldName('username')
->setMessage('Username cannot be blank')
->addConstraint(Constraint::notBlank())
)
;
$validator = new Validator();
$validator->setRuleSet($ruleSet);
useDCP\Form\Validation\Validator;
useDCP\Form\Validation\Rule;
useDCP\Form\Validation\Constraint;
$validator = new Validator();
$validator->addRule(
(new Rule())
->setFieldName('username')
->setMessage('Username cannot be blank')
->addConstraint(Constraint::notBlank())
);
useDCP\Form\Validation\Validator;
useDCP\Form\Validation\Rule;
useDCP\Form\Validation\Constraint;
$validator = new Validator();
$validator->addRule(
(new Rule())
->setFieldName('username')
->setMessage('Username cannot be blank')
->addConstraint(Constraint::notBlank())
);
$form = [
'username' => 'test_user'
];
$result = $validator->validate($form);
if ($result->isValid()) {
echo'Hooray, the form is valid!';
}
useDCP\Form\Validation\Validator;
useDCP\Form\Validation\Rule;
useDCP\Form\Validation\Constraint;
classMyForm{
protected $username;
publicfunctiongetUsername(){
return$this->username;
}
publicfunctionsetUsername($username){
$this->username = $username;
}
}
$validator = new Validator();
$validator->addRule(
(new Rule())
->setFieldName('username')
->setMessage('Username cannot be blank')
->addConstraint(Constraint::notBlank())
);
$form = new MyForm();
$form->setUsername('test_user');
$result = $validator->validate($form);
if ($result->isValid()) {
echo'Hooray, the form is valid!';
}
(new Rule())
->setFieldName('username')
->setMessage('Username must not be blank')
->addConstraint(Constraints::notBlank())
;
(new Rule())->getMessage();
// This field failed validation
(new Rule())
->setFieldName('password_repeat')
->setMessage('Passwords must match')
->addPrerequisite(Prerequisites::notBlank(new FieldReference('password')))
->addFilter(Filters::trim())
->addConstraint(Constraints::notBlank())
;
$ruleSet = (new RuleSet())
->add(
(new Rule())
->setFieldName('username')
->setMessage('Username cannot be blank')
->addConstraint(Constraint::notBlank())
)
;
$validator = new Validator();
$validator->setRuleSet($ruleSet);
$validator = new Validator();
$validator->addRule(
(new Rule())
->setFieldName('username')
->setMessage('Username cannot be blank')
->addConstraint(Constraint::notBlank())
);
(new Rule())
->setFieldName('email_address')
->addConstraint(Constraints::notBlank())
->addConstraint(Constraints::formatEmail())
;
// This will ensure that the email address is not blank, and matches a valid email format (e.g. user@example.com)
(new Rule())
->setFieldName('username')
->addFilter(Filters::trim())
->addFilter(Filters::toLowerCase())
;
// This will trim and lowercase the username field prior to validating the rule.
/*
* Assume the form has a field named 'send_me_an_email_newsletter' that is a checkbox with a value
* of 'yes'. When checked, the user is ->addPrerequisite(Prerequisites::mustMatch(new FieldReference('send_me_an_email_newsletter'), 'yes'))
->addFilter(Filters::trim())
->addConstraint(Constraints::notBlank())
->addConstraint(Constraints::formatEmail())
)
;
(new RuleSet())
->add(
(new Rule())
->setFieldName('password')
->setMessage('Password must not be blank')
->addFilter(Filters::trim())
->addConstraint(Constraints::notBlank())
)
->add(
(new Rule())
->setFieldName('password_repeat')
->setMessage('Passwords do not match')
// Do not validate unless the password field is not blank.
->addPrerequisite(Prerequisites::notBlank(new FieldReference('password')))
->addFilter(Filters::trim())
// This field must match the password field's value.
->addConstraint(Constraints::mustMatch(new FieldReference('password')))
)
;
$result = $validator->validate($form);
if ($result->isValid()) {
echo'Hooray, the form is valid!';
} else {
echo'Better luck next time.';
}
$result = $validator->validate($form);
if (!$result->isValid()) {
if ($result->fieldHasError('username')) {
echo'Username field had a validation error.';
}
}
$result = $validator->validate($form);
if (!$result->isValid()) {
if ($result->fieldHasError('username')) {
echo"Username had a validation error. Wonder what it was:\n";
echo $result->getError('username');
}
}
// From the Constraints classpublicstaticfunctionnotBlank(){
returnfunction($data){
return strlen($data) > 0;
};
}
$steveCallback = function($data){
if ($data === 'steve') {
returntrue;
}
returnfalse;
};
(new Rule())
->setFieldName('username')
->setMessage('You are definitely not steve')
->addConstraint($steveCallback)
;
classMyConstraintsextendsConstraints{
publicstaticfunctionisSteve(){
returnfunction($data){
if ($data === 'steve') {
returntrue;
}
returnfalse;
};
}
}
(new Rule())
->setFieldName('username')
->setMessage('You are definitely not steve')
->addConstraint(MyConstraints::isSteve())
;
(new Rule())
->setFieldName('username')
->setMessage('Please enter a username')
->addConstraint(Constraints::notBlank())
->addValidationGroup('page_1')
;
$rules = (new RuleSet())
->add(
(new Rule())
->setFieldName('username')
->setMessage('Please enter a username')
->addConstraint(Constraints::notBlank())
->addValidationGroup('page_1')
)
->add(
(new Rule())
->setFieldName('password')
->setMessage('Please enter a password')
->addConstraint(Constraints::notBlank())
->addValidationGroup('page_2')
)
;
$form = [
'username' => '',
'password' => ''
];
$validator = new Validator();
$validator->setRuleSet($rules);
$validator->validate('page_2');
// This will only validate the password field.
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.