Step 8 - Add a validation¶
Tip
You can find all files used and modified in this step on GitHub.
Now you will add the option to configure a list of authors whose tweets are allowed. To achieve this, you have to:
- implement
validateValidatorConfiguration()
andvalidate()
methods in the Type class - implement an additional interface in the FormMapper
- add field definition edit view
- implement
toStorageFieldDefinition()
andtoFieldDefinition()
methods in LegacyConverter
Implement validateValidatorConfiguration()
and validate()
methods in the Type class¶
validateValidatorConfiguration()
will be called when an instance of the Field Type is added to a Content Type,
to ensure that the validator configuration is valid.
For the validator schema configuration, add the following to eZ/Publish/FieldType/Tweet/Type.php
:
1 2 3 4 5 6 7 8 |
|
For a TextLine (length validation), it means checking that both minimum and maximum lengths are positive integers, and that minimum is lower than maximum.
When an instance of the type is added to a Content Type,
validateValidatorConfiguration()
receives the configuration for the validators used by the Type as an array.
This method must return an array of error messages if errors are found in the configuration,
and an empty array if no errors were found.
For the TextLine Field Type included in eZ Platform, an example array passed to validateValidatorConfiguration()
method looks like this:
1 2 3 4 5 6 |
|
The structure of this array depends on each Field Type implementation. The best practice is to mimic what is done in native Field Types.
Each level one key is the name of a validator, as acknowledged by the Type. That key contains a set of parameter name / parameter value rows. You must check that:
- all the validators in this array are known to the type
- arguments for those validators are valid and have correct values
You do not need to include mandatory validators if they don't have options. Here is an example of what the Type expects as validation configuration:
1 2 3 4 5 |
|
The configuration says that tweets must be either by johndoe
or by janedoe
.
If you had not provided TweetValueValidator
at all, it would have been ignored.
You will iterate over the items in $validatorConfiguration
and:
- add errors for validators you don't know about
- check that provided arguments are known and valid (
TweetValueValidator
accepts a non-empty array of valid Twitter usernames)
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 |
|
validate()
is the method that runs the actual validation on data:
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 35 36 37 38 |
|
Earlier you validated the URL with a regular expression.
Now, if the configuration of the Field Type's instance contains a TweetValueValidator
key,
you will check that the username in the status URL matches one of the valid authors.
Implement FieldDefinitionFormMapperInterface in FormMapper¶
Now you need to offer a way to the user to input a list of authors (upon which the data will be validated).
To achieve this, you will add additional functionality in FormMapper
that allows you to define the necessary input field.
This is a minimal example of eZ/Publish/FieldType/Tweet/FormMapper.php
:
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 |
|
In this case, the TweetValueValidator
expects authorList
to be an array.
On the other hand, the input field has TextType, so it will return a string.
To solve this, transform data from an array to a comma-separated string and the other way using a DataTransformer
.
Create TweetFieldTypeBundle/Form/StringToArrayTransformer.php
:
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 |
|
Then, you can use this DataTransformer
in the FormMapper
like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
FormMapper
right now works also as FieldDefinitionFormMapper
.
In order to do that add an extra tag definition in Resources/config/fieldtypes.yml
:
1 2 3 4 5 6 |
|
Add field definition edit view¶
You have the new part of the form defined, but you still need to show it to the user.
To do that, create a file containing the view, Resources/views/platformui/content_type/edit/eztweet.html.twig
:
1 2 3 4 5 6 7 |
|
Register the new template in the configuration by editing the Resources/config/ez_field_templates.yml
file:
1 2 |
|
Implement toStorageFieldDefinition()
and toFieldDefinition()
methods in LegacyConverter¶
The last thing to do is to make sure that validation data is properly saved into and retrieved from the database.
To achieve this, implement these two functions in eZ/Publish/FieldType/Tweet/LegacyConverter
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
toStorageFieldDefinition()
converts a Field definition to a legacy one using the dataText1
field
(which in this example means converting it to JSON).
toFieldDefinition()
converts a stored legacy field definition to an API Field definition
(which means converting it back to an array according to validation schema).
You should now be able to configure a list of authors when editing a Content Type with the Tweet Field Type. You should also have tweets validated in accordance to this list when you create or edit Content item with this Field Type.