Performing entity validation
In this recipe, we will walk through entity validation. Drupal has integrated with the Symfony Validator component. Entities can be validated before saving. We will build off of the last recipe, which allows updating an article node to add validation of its values.
How to do it…
- We will be adding validation to the
update
method from the previous recipe:public function update(Request $request,
NodeInterface $node): JsonResponse {
$content = $request->getContent();
$json = \Drupal\Component\Serialization\
Json::decode($content);
if (isset($json['title'])) {
$node->setTitle($json['title']);
}
if (isset($json['body'])) {
$node->set('body', $json['body']);
}
...