Problem
For being able to solve #2395831: Entity forms skip validation of fields that are not in the EntityFormDisplay (critical) we need to be able to define general, entity-level validation constraints, which run regardless of which fields are being edited. Our use case in core is the "changed" validation, which needs to be converted. Further possible uses could be constraints for #2105797: Figure out the best way to do conditional validation.
Proposed resolution
- Allow defining entity level constraints via the entity type annotation (as it works for data types)
- Allow defining entity level constraints per hook:
hook_entity_type_build()
- Allow altering entity level constraints
hook_entity_type_alter()
- Extend the ContentEntityType object with a
getConstraints()
method - Apply entity constraints by defining type constraints at the
EntityDeriver
or inEntityDataDefinition::getConstraints()
- Remove the EntityType and Bundle constraints from entity objects as they're already added by EntityDataDefinition::{setEntityTypeId|setBundle}
- Do not add the constraints of referenced data in in TypedDataManager::getDefaultConstraints any more - not all of those constraints might be relevant for validating the referenced data is right, but might validate the data itself. Instead just add constraints to the the data reference property (entity, language) directly, what will now work nicely as the passed on data is always the same.
Remaining tasks
Do it.
User interface changes
-
API changes
Only additions.
Allow adding entity level constraints for general validation logic that should run for all changes (e.g. entity changed validation)