- Documentation >
- Guide >
- Content management >
- Page >
- Page block validators
Page block validators
Validators check values passed to Page block attributes.
The following block validators are available:
required
- checks whether the attribute is provided
regexp
- validates attribute according to the provided regular expression
not_blank
- checks whether the attribute is not left empty
not_blank_richtext
- checks whether a richtext
attribute is not left empty
content_type
- checks whether the selected Content Types match the provided values
content_container
- checks whether the selected Content item is a container
Note
Do not use the required
and not_blank
validators for richtext
attributes.
Instead, use not_blank_richtext
.
For each validator you can provide a message that displays in the Page Builder
when an attribute field does not fulfil the criteria.
Additionally, for some validators you can provide settings in the options
key, for example:
| email:
type: string
name: E-mail address
validators:
regexp:
options:
pattern: '/^\S+@\S+\.\S+$/'
message: Provide a valid e-mail address
|
Custom validators
You can create Page block attributes with custom validators.
The following example shows how to create a validator which requires that string attributes contain only alphanumerical characters.
First, create classes that support your intended method of validation.
For example, in src/Validator
, create an AlphaOnly.php
file:
| <?php declare(strict_types=1);
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
class AlphaOnly extends Constraint
{
public $message = 'The attribute can only contain letters or numbers.';
}
|
In src/Validator
, create an AlphaOnlyValidator.php
class that performs the validation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 | <?php declare(strict_types=1);
namespace App\Validator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
class AlphaOnlyValidator extends ConstraintValidator
{
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof AlphaOnly) {
throw new UnexpectedTypeException($constraint, AlphaOnly::class);
}
if (null === $value || '' === $value) {
return;
}
// Throw an exception if the validator cannot handle the passed type.
if (!is_string($value)) {
throw new UnexpectedValueException($value, 'string');
}
if (!preg_match('/^[a-zA-Z]+$/', $value, $matches)) {
// The argument must be a string or an object implementing __toString()
$this->context->buildViolation($constraint->message)
->setParameter('{{ string }}', $value)
->addViolation();
}
}
}
|
Then, in config/packages/ezplatform_page_fieldtype.yaml
enable the new validator in Page Builder:
| ezplatform_page_fieldtype:
block_validators:
alpha_only: 'App\Validator\AlphaOnly'
|
Finally, add the validator to one of your block attributes in config/packages/ezplatform_page_fieldtype.yaml
, for example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | ezplatform_page_fieldtype:
blocks:
my_block:
name: My Block
category: default
thumbnail: /bundles/ibexaplatformicons/img/all-icons.svg#edit
views:
default:
name: Default block layout
template: my_block.html.twig
priority: -255
attributes:
my_text_attribute:
type: text
name: My text attribute
validators:
alpha_only:
message: The field can only contain letters or numbers.
|