Try our conversational search powered by Generative AI!

Conditional Validation on a BlockType/PageType

Ayo
Ayo
Vote:
 

I have a Block Type wich has these two properties.

[CultureSpecific]
[Display(
Name = "Display PDF Button", GroupName = TabNames.PDFCustomisation, Order = 0)]
public virtual bool DisplayPdfButton { get; set; }

[CultureSpecific]
[Required]
[Display(
Name = "Download Pdf Text", GroupName = TabNames.PDFCustomisation, Order = 1)]
public virtual string DownloadPdfText { get; set; }



I only want DownloadPdfText to be required if the user sets DisplayPdfButton to True. - Is this possible to do in Episerver?

#190514
Apr 11, 2018 15:14
Vote:
 

You can create your own validator class by implementing EPiServer.Validation.IContentSaveValidate<T>. You cn find more info here https://world.episerver.com/documentation/developer-guides/CMS/Content/Validation/

#190588
Apr 12, 2018 12:45
Vote:
 

Remove the required-attribute on DownloadPdfText, and add this class. Change CustomBlockType to your blocktype.

    public class CustomBlockValidator : IValidate<CustomBlockType>
    {
        IEnumerable<ValidationError> IValidate<CustomBlockType>.Validate(CustomBlockType customBlockType)
        {
            if (customBlockType.DisplayPdfButton && string.IsNullOrEmpty(customBlockType.DownloadPdfText))
            {
                return new[]
                        {
                            new ValidationError
                            {
                                ErrorMessage = "You can't hava a button without text, stupid!",
                                PropertyName = CustomBlockType.GetPropertyName(block => block.DownloadPdfText),
                                Severity = ValidationErrorSeverity.Error,
                                ValidationType = ValidationErrorType.AttributeMatched
                            }
                        };
            }
            

            return Enumerable.Empty<ValidationError>();
        }
    }
#190590
Edited, Apr 12, 2018 13:06
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.