-
Notifications
You must be signed in to change notification settings - Fork 2
Add custom validator
Spafaridis Xenofon edited this page Jan 2, 2016
·
1 revision
Define a validator class that extends BaseValidator
class AddressValidator extends \Phramework\Validate\StringValidator
{
/**
* Overwrite base class type
* @var string
*/
protected static $type = 'x-address';
public function __construct(
$minLength = 0,
$maxLength = null
) {
parent::__construct(
$minLength,
$maxLength,
'/^[A-Za-z0-9 ]+ [0-9]+$/'
);
}
}
Then register this validator
BaseValidator::registerValidator(
AddressValidator::getType(),
AddressValidator::class
);
You can create instances of this validator from json.
$json = '{
"type": "x-address",
"minLength" : 4,
"maxLength" : 20
}';
$validationObject = BaseValidator::createFromJSON($json);
$address = $validationObject->parse('Hlektras 6');