Try our conversational search powered by Generative AI!

Validation on a BlockType - no errormessage in UI when saving block

Vote:
 

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.

#222453
May 06, 2020 6:59
Vote:
 

Do you see any JavaScript errors in your browsers console?

I slightly changed your code, and tested this. It worked. 

public class ImageBlockValidator : IValidate<ImageBlock>
{
    public IEnumerable<ValidationError> Validate(ImageBlock imageBlock)
    {
        if (imageBlock.Image != null && string.IsNullOrEmpty(imageBlock.ImageAlt))
        {
            yield return new ValidationError
            {
                ErrorMessage = "Oh no! Gimme some alt!",
                PropertyName = nameof(imageBlock.ImageAlt),
                RelatedProperties = new[] { nameof(imageBlock.Image) },
                Severity = ValidationErrorSeverity.Error,
                ValidationType = ValidationErrorType.StorageValidation
            };
        }
    }
}

Episerver.Cms.Core 11.15.0

#222461
Edited, May 06, 2020 8:14
Vote:
 

Thanks for your help but it still doesn't work.
There is a javascript error when clicking the save button:

Object { message: "Unable to load /EPiServer/cms/Stores/contentdata/ status: 409", stack: "_317@http://localhost:90/EPiServer/Shell/11.23.6/ClientResources/dojo/dojo.js:15:78148\n_4f2@http://localhost:90/EPiServer/Shell/11.23.6/ClientResources/dojo/dojo.js:15:133806\n_4fe@http://localhost:90/EPiServer/Shell/11.23.6/ClientResources/dojo/dojo.js:15:134085\n", response: {…}, status: 409, responseText: "The page was not displayed because there was a conflict.", xhr: XMLHttpRequest }
dojo.js:15:123181
#222479
May 06, 2020 12:47
Vote:
 

If you create your block from assets pane, it should work.

Same issue as described here: https://world.episerver.com/forum/developer-forum/-Episerver-75-CMS/Thread-Container/2017/12/409-conflict-creating-block/

#222480
May 06, 2020 12:49
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.