The configuration of custom validation involves the use of the ExecutionContext::addViolation() function.
This function takes a message as its first parameter. If the message parameter is left empty it results in an exception. that states that the message parameter cannot be null. Instead the null message value should be allowed and automatically replaced by a default generic message.
Below is the error while we creating the custom validation without a message.Drupal\Core\Validation\ExecutionContext::addViolation(): Argument #1 ($message) must be of type string, null given,
Steps to reproduce
Create a custom constraint in accordance with the relevant Drupal documentation
Create a custom constraint
Configure a custom validation using the ExecutionContext::addViolation() function. Leave out the message parameter at this position in the code:$this->context->addViolation($constraint->notInteger, ['%value' => $item->value]);
Trigger the validation error message by trying to save a non-unique integer to a test entity. The test entity contains an integer field that the custom constraint is bound to.
Proposed resolution
Typehint the message parameter of the ExecutionContext::addViolation() function to allow a string or a null value. Create an if statement so that if the message parameter has a null value, meaning it is not supplied, supply a generic message.
Remaining tasks
Add test coverage
User interface changes
N/A
Introduced terminology
N/A
API changes
Custom constraints will no longer require a message to be specified in which case a short, generic message will be used.
Data model changes
N/A