1. Go to this page and download the library: Download broeser/wellid 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/ */
broeser / wellid example snippets
/*
* The value to validate
*/
$value = 'somethingentered';
/*
* The validator that shall be used to validate it
*/
$maxLengthValidator = new Wellid\Validator\MaxLength(7);
/*
* Validate the value and do something in case it is valid:
*/
if($maxLengthValidator->validateBool($value)) {
print('The given value '.$value.' fits our
/*
* The value to validate
*/
$value = 'somethingentered';
/*
* The validator that shall be used to validate it
*/
$maxLengthValidator = new Wellid\Validator\MaxLength(7);
/*
* Validate the value and get a ValidationResult-object
*/
$validationResult = $maxLengthValidator->validate($value);
if($validationResult->hasPassed()) {
print('The given value '.$value.' fits our
/*
* The value to validate
*/
$value = 'somethingentered';
/*
* The validator that shall be used to validate it
*/
$maxLengthValidator = new Wellid\Validator\MaxLength(7);
$minLengthValidator = new Wellid\Validator\MinLength(3);
$validationResultSet = new Wellid\ValidationResultSet();
$validationResultSet->add($maxLengthValidator->validate($value));
$validationResultSet->add($minLengthValidator->validate($value));
if($validationResultSet->hasPassed()) {
print('The given value '.$value.' fits our
class AccountBalance implements \Wellid\ValidatableInterface {
use \Wellid\ValidatableTrait;
/**
* @var float
*/
protected $value = null;
public function __construct() {
$this->addValidators(new \Wellid\Validator\FloatingPoint(), new \Wellid\Validator\Min(0), new \Wellid\Validator\Max(830));
}
/**
* @param float $val
* @return \self
*/
public static function createFromFloat($val) {
$newInstance = new self();
$newInstance->setValue($val);
return $newInstance;
}
/**
* @return float
*/
public function getValue() {
return $this->value;
}
/**
* @param float $val
*/
public function setValue($val) {
$this->value = $val;
}
}
foreach(array(57.3, -6) as $v) {
$yourBalance = WellidUsageExamples\AccountBalance::createFromFloat($v);
$result = $yourBalance->validate();
if($result->hasErrors()) {
print('Oh dear! Something invalid was used as my account balance!'.PHP_EOL);
print('Aha, that is why: '.$result->firstError()->getMessage().PHP_EOL);
}
}
class AccountBalanceValidators extends \Wellid\ValidatorHolder {
public function __construct() {
$this->addValidators(new \Wellid\Validator\FloatingPoint(), new \Wellid\Validator\Min(0), new \Wellid\Validator\Max(830));
}
}
$accountBalanceValidators = new \WellidUsageExamples\AccountBalanceValidators();
foreach(array(57.3, -6) as $v) {
$result = $accountBalanceValidators->validateValue($v);
if($result->hasErrors()) {
print('Oh dear! Something invalid was used as my account balance!'.PHP_EOL);
print('Aha, that is why: '.$result->firstError()->getMessage().PHP_EOL);
}
}
/*
* Example 3b: Using the ValidatorHolderTrait & ValidatorHolderInterface with
* data objects
*/
$accountBalanceValidators = new \WellidUsageExamples\AccountBalanceValidators();
foreach(array(57.3, -6) as $v) {
$yourBalance = WellidUsageExamples\AccountBalance::createFromFloat($v);
$result = $accountBalanceValidators->validateValue($yourBalance->getValue());
if($result->hasErrors()) {
print('Oh dear! Something invalid was used as my account balance!'.PHP_EOL);
print('Aha, that is why: '.$result->firstError()->getMessage().PHP_EOL);
}
}
class SanitorWellidEmailExample implements \Wellid\SanitorBridgeInterface, \Wellid\ValidatableInterface {
use \Wellid\SanitorBridgeTrait, \Wellid\ValidatableTrait;
/**
* Constructor
*
* @param \Sanitor\Sanitizer $sanitizer
*/
public function __construct(\Sanitor\Sanitizer $sanitizer = null) {
$this->setSanitizer(is_null($sanitizer)?new \Sanitor\Sanitizer(FILTER_SANITIZE_EMAIL):$sanitizer);
$this->addValidator(new \Wellid\Validator\Email());
}
}
$emailValidator = new WellidUsageExamples\SanitorWellidEmailExample(new \Sanitor\Sanitizer(FILTER_SANITIZE_EMAIL));
$emailValidator->setRawValue('mail@benedict\roeser.de'); // because the value will be sanitized before validation, this will actually pass! More information in the "65{"-example below
if($emailValidator->validate()->hasErrors()) {
print('Why! Oh why! Errors everywhere!'.PHP_EOL);
}
// values can also be optained from INPUT_GET, INPUT_POST, etc.:
$emailValidator->rawValueFromInput(INPUT_REQUEST, 'email');
if($emailValidator->validate()->hasPassed()) {
print('Nice, this input has been valid: '.$emailValidator->getValue().PHP_EOL);
}