Is there a way to give a character limit to XHTMLString and strings?

Vote:
 

Hi,

At the moment our client is experiencing an issue when translating content using a third party plugin, which says that "The field Title must be a string or array type with a maximum length of '50'"

Is there any Data Annotation that i could use on a XHTMLString/String or would i need to create my own Validation method?

Thanks!

#310778
Edited, Oct 13, 2023 12:42
Vote:
 

You can use System.ComponentModel.DataAnnotations.StringLengthAttribute for string properties. But for XhtmlString properties you would need to roll your own validator.

#310820
Oct 14, 2023 6:01
Vote:
 

I've blogged on this a while ago, https://www.herlitz.io/2022/12/21/validate-optimizely-xhtmlstring-property-length/

It'll enable you to have a minimum amount of characters as well as a maximum amount.

[Display(Name = "MainContent")]
[Required]
[XhtmlStringLengthValidation(50, 500, CustomErrorMessage = "The Main content field must contain more than {0} characters but less than {1}.")]
public virtual XhtmlString MainContent{ get; set; }

It should as well only count text characters not including the markup.

#310899
Oct 16, 2023 8:45
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.