FieldValidationException is thrown when https://api.drupal.org/api/drupal/modules!field!field.attach.inc/functio... is called to validate field data prior to saving an entity via the API.
Callers are expected to catch the exception and provide output that is meaningful to their circumstances.
The problem is that the exception is not very useful. Its message is simply 'Field validation errors', which tells you nothing. The exception has an $errors array, which has the full details of field, language, delta, and the nature of the error, but that's a deeply nested array.
It seems pretty poor DX to require users of this to iterate into this array to get a message out. Eg, I've encountered this in both Services and Migrate, and rather than implement identical code in both, the sensible thing to me seems to improve the exception message.
Comment | File | Size | Author |
---|---|---|---|
#1 | 2226495.drupal.FieldValidationException-message.patch | 872 bytes | joachim |
Comments
Comment #1
joachim CreditAttribution: joachim commentedNot brilliant, as the message inside the array has placeholders and I am not sure how best to handle the t().
Comment #2
stefan.r CreditAttribution: stefan.r commentedThis patch looks good to me. The message inside error_messages is already wrapped in t()'s so I don't see how it's "not brilliant" :)
Comment #3
qjensen CreditAttribution: qjensen commentedThis patch works well for me. It certainly makes troubleshooting significantly easier. What needs to happen for this patch to be committed?