-
Notifications
You must be signed in to change notification settings - Fork 5
validators
Aeria validates fields while editing the frontend, calling our validation API, and while saving the fields in the backend.
Aeria provides a simple way to validate the edited fields. By default, two validators are integrated in Aeria:
-
"isEmail"
checks if the inserted value is a valid email, using PHP'sFILTER_VALIDATE_EMAIL
-
"isShort"
checks if the inserted string is too short (length<4)
We obviously thought you may need new validators, so here are two dummy-proof ways of creating new validators.
Let's say we want to validate our book author by checking if there are no special characters in his name. The regular expression we need is something like this: /^\w*$/
. Now, we only need to extend AbstractRegExValidator
with our AuthorValidator
.
RegEx validators need 3 properties:
-
$_key
is our validator name. We're gonna useisAuthor
. -
$_message
is the error message displayed when this validator is not satisfied. -
$_validator
is our RegEx. Don't forget PHP's RegExs delimiter/
.
Now we're able to create our validator class in functions.php
.
if (function_exists('aeria')) {
class AuthorValidator extends Aeria\Validator\Types\RegEx\AbstractRegExValidator
{
protected static $_key="isAuthor";
protected static $_message="Please, insert a valid author.";
protected static $_validator="/^\w*$/";
}
aeria('validator')->registerValidatorClass(AuthorValidator::class);
}
After declaring the class, we need to register the validator in Aeria by calling registerValidatorClass
.
Now that the validator is registered, we just need to add it to the author field.
{
"type": "text",
"id": "author-name",
"label": "Author name",
"description": "Insert the book's author name here.",
"size": "half",
"placeholder": "Author",
"validators": "isAuthor",
"required": true
}
Sometimes your validation cannot just be a simple RegEx. Aeria offers you the possibility to declare validators with callable methods inside them.
Let's say we now want to check if the book's cover image exists. A RegEx cannot obviously do this.
We need to extend Aeria\Validator\Types\Callables\AbstractValidator
with our BookCoverValidator
.
Like the RegEx validator, a callable validator needs:
- a
$_key
to access the validator, let's say,"isBookCover"
. - an error
$_message
to display the user. - a
getValidator
method returning a function.
The function returned by getValidator
has one argument, $field
, and returns an array composed of:
-
"status"
which may be- false if the field is ok.
- true if the field is not valid.
-
"message"
just in case"status"
is true, containing the error message.
Now we're able to create our validator class in functions.php
.
if (function_exists('aeria')) {
class BookCoverValidator extends Aeria\Validator\Types\Callables\AbstractValidator
{
protected static $_key="isBookCover";
protected static $_message="Please insert a valid book cover.";
public static function getValidator(){
return function ($photoID)
{
if (wp_get_attachment_image($photoID) != null)
return ["status" => false];
else
return ["status" => true,"message" => static::$_message];
};
}
}
aeria('validator')->registerValidatorClass(BookCoverValidator::class);
}
After declaring the class, we need to register the validator in Aeria by calling registerValidatorClass
.
Now that the validator is registered, we just need to add it to the book cover field.
{
"type": "picture",
"id": "book-cover",
"label": "Book cover",
"description": "Insert the book's cover",
"size": "full",
"required": true,
"ctaLabel": "Insert cover",
"validators": "isBookCover"
}
Our validator is now up and running.
You may want to validate your fields before saving them in the backend. We exposed a validation API that does just that.
To validate a field with a validators list, you just need two parameters:
-
field
is the value of the field. -
validators
contains all the validators, separated by a|
.
The response is an array containing
- if the validation was not passed, a
"message"
array containing all the error messages. - a
"status"
boolean,false
if the field is valid,true
if not. - a
"value"
string, containing the checked value.
The API can be accessed on the path http://example.com/wp-json/aeria/validate
.
An example call to this validation API may be:
http://example.com/wp-json/aeria/validate?field=johnsmithatexample.com&validators=isEmail|isShort
, which would return:
{
"message": [
"Please insert a valid email. "
],
"value": "johnsmithatexample.com",
"status": true
}
Sometimes you just don't wanna cope with validators lists. Know the field's ID? That's all you need.
You can use this validator at http://example.com/wp-json/aeria/validate-by-id
, with the parameters:
-
field_id
is the value of the field's full ID, containing the metabox ID. This is the key you can see in the database. -
value
is the value you want to validate.
The response is equal to the one above, so an array containing:
- if the validation was not passed, a
"message"
array containing all the error messages. - a
"status"
boolean,false
if the field is valid,true
if not. - a
"value"
string, containing the checked value.
An example call to this validation API may be:
http://example.com/wp-json/aeria/validate-by-id?field_id=metaexample-input-text-email&value=aeria%example,com
, which would return:
{
"message": [
"Please insert a valid email. "
],
"value": "aeria%example,com",
"status": true
}
A valid email, would instead return:
{
"message": [],
"value": "[email protected]",
"status": false
}