We have a block with properties Image and ImageAlt. To get the ImageAlt to be required if the user add an Image we created a custom validation class for the block.
public class ImageBlockValidator : IValidate<ImageBlock>
{
public IEnumerable<ValidationError> Validate(ImageBlock instance)
{
if (instance.Image != null && string.IsNullOrEmpty(instance.ImageAlt))
{
return new[] { new ValidationError
{
ErrorMessage = LocalizationService.Current.TranslateKey("/validationmessages/imageblock/imagealt/heading"),
PropertyName = instance.GetPropertyName(block => block.ImageAlt),
RelatedProperties = new[] { "Image" },
Severity = ValidationErrorSeverity.Error,
ValidationType = ValidationErrorType.StorageValidation
}};
}
return new ValidationError[0];
}
}
But when a user add an Image and click "Spara" no error message is shown i the UI, the "Spara" button gets inactive and nothing happens. The block isn´t saved.
But if the user saves the block before adding an Image and then goes back to edit the block and adds an Image the validation error message is shown.