Our monthly Virtual Happy Hour is happening this week, Fri Feb 23!

Try our conversational search powered by Generative AI!

´Validating Link Item

Vote:
 

Hi, 

I'm working in a validator for LinkItem. 
It does validate what I want and it works when I'm debugging it. 

However it does not work like it should in the CMS. 
Instead of showing my warning message, it displays a popup with this text: 

Could not save property. The page may have been changed by another user. Please reload the page.

It seems to be something with the scoping, I had the same issue with another validator for an Image block. That did work when the image block was standalone but not when the block was used as a property, and I guess it's the same with the LinkItem property. 

Here's how my validator is built:

public class LinkItemValidator : IValidate<LinkItem>
{
     public IEnumerable<ValidationError> Validate(LinkItem instance)
     {
           //bla bla bla check
            yield return new()
            {
                RelatedProperties = new[] { nameof(instance.Text) },
                ErrorMessage = "Error message",
                Severity = ValidationErrorSeverity.Warning,
                ValidationType = ValidationErrorType.Unspecified
            };
     }
}

Does anybody know how I can validate the text on LinkItem and other similar properties?  


Thanks.

#313347
Nov 29, 2023 11:55
Vote:
 

I have mostly used page or block instance to validate a specific property instead of proprety type instance itself. Looking at the code I believe yield is the issue here you may want to check. If you use this in the validate method it should work.

            var errorList = new List<ValidationError>();

            if (instance != null)
            {
                errorList.Add(new ValidationError
                {
                    ErrorMessage = $"Link item text is {instance.Text}",
                    Severity = ValidationErrorSeverity.Error
                });
            }

            return errorList

#313658
Edited, Dec 04, 2023 15:16
Vote:
 

Thanks for your response. 

Unfortunatey your suggestion throws the same error so there has to be something else that's off. 

#313696
Dec 05, 2023 7:13
Vote:
 

Hey oliaxe,

can you specify the major version of the CMS you are working with (and ideally update the thread's tags as well). I am still in the 11 world and suspect you are referring to version 12 since I found out it there it was made much simpler to work with LinkItem instances directly.

Best

#314080
Dec 12, 2023 10:31
* 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.