Hi there!
Currently making some test with a headless D8 and trying out the Entity Validation API.
I am using only the server side validation.
Based on the returned error, I need to map the error with the field having the error.
I have been playing with validation on title field, here is what I got:
When no data in title, I get this error:
Unprocessable Entity: validation failed. title: This value should not be null.
When adding constraint:
function mymodule_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
if ($entity_type->id() == 'node' && $bundle == 'customer') {
$fields['title']->addPropertyConstraints('value', ['Length' => ['min' => 3, 'max' => 10]]);
}
}
I get this error:
Unprocessable Entity: validation failed. title.0.value: This value is too short. It should have 3 characters or more.
The field key are not the same. Is there any way to have the same structure? That would help a lot!
Also, is there a way to get the errors not formatted. Right now, I get a string, which I will have to manually split to extract the field ID and the error. It would be much simpler if I was able to get an array containing all errors, keyed by field ID for example.
Thanks!
Ed
Comments
Comment #6
cilefen commentedI am closing this support request because there have been no recent comments.
The Drupal Core issue queue is not the ideal place for support requests. Consider other sources of support.