Try our conversational search powered by Generative AI!

Set Default Required Validator in Custom form element

Vote:
 

Hi Team,

Is there a way to set the Custom form element required validator to true, so that it will not be necessary for the user to set it fom the CMS everytime they add.

I have created a custom form element inheriting from TextBlockElementBlock. During SetDefaultValues i want to set the Required validator to true as default, but not sure how to do that.

#225443
Jul 16, 2020 13:09
Vote:
 

You can override the mehod SetDefaultValues and set value for Validator property which should be RequireValidator. Something looks like:

public override void SetDefaultValues(ContentType contentType)
{
	base.SetDefaultValues(contentType);
	Validators = typeof(RequiredValidator).FullName;
}
#225469
Jul 17, 2020 6:21
Swati - Jul 17, 2020 8:34
I tried this:

public override void SetDefaultValues(ContentType contentType)
{
base.SetDefaultValues(contentType);
Label = "First Name";
PlaceHolder = "John";
this.Validators = typeof(RequiredValidator).FullName;
this.Validators = typeof(EmailValidator).FullName;
}
But it picks the last one, not both.
Dac Thach Nguyen - Jul 17, 2020 8:38
You need to concat values into a string: this.Validators = string.Concat(typeof(RequiredValidator).FullName, EPiServer.Forms.Constants.RecordSeparator, typeof(EmailValidator).FullName);
Valina Eckley - Jul 28, 2020 23:05
Dac - This is pretty much what I'm trying to do, but I'm trying to fill in a custom regex value. I can use typeof(RegularExpressionValidator).FullName to get the checkbox filled, and I'm pretty sure you are supposed to use RegularExpressionAttribute to set the value of the regex, but I can't seem to figure out how to do it. Any advice?
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.