Programatically created forms and required fields

Vote:
 

Hello,

I have a page type which automatically creates an Episerver form including multiple form elements and includes it in a ContentArea. Smooth sailing so far. But when I try to make one of these generated fields required by default nothing quite seems to work.

I have tried variations of the following... 

		private void AddCheckBox(string label, FormContainerBlock container, IContentRepository repo)
		{
			var checkBox = repo.GetDefault<ChoiceElementBlock>(ContentReference.GlobalBlockFolder);
			var option = new OptionItem()
			{
				Caption = label
			};

			checkBox.Items = new List<OptionItem>() { option };
			checkBox.AllowMultiSelect = true;
			
			checkBox.Validators = "EPiServer.Forms.Implementation.Validation.RequiredValidator";
			
			var reference = repo.Save((IContent)checkBox, SaveAction.Publish);

			container.ElementsArea.Items.Add(new ContentAreaItem()
			{
				ContentLink = reference
			});
		}

This does make the field required in the CMS view. But on the rendered view, even if the user has checked the checkbox it still spits out the "This field is required" message.

I can find little documentation on the subject and it'd be great to hear if someone has had similiar problems and any potential steps taken to solve it?

-------

This is how the EPiServer.Forms.Implementation.Elements.BaseClass.ValidateableElementBlockBase.Validators property is documented. Presumably this string can be set manually when generating the form, but the format in which it should be structured is completely foreign to me.

Documentation

List of validator settings for this FormsElement. This is used to store validator settings (information). This is used to be implemented as a property of Block. This will store values in its property like "EPiServer.Forms.Implementation.Validation.RequiredValidator|||EPiServer.Forms.Implementation.Validation.RegularExpressionValidator###111"
#250992
Mar 19, 2021 10:01
Vote:
 

Have you tried like 

chkbox.Validators = typeof(EPiServer.Forms.Implementation.Validation.RequiredValidator).FullName;

The following article is very useful in creating a form using API

http://www.aperturelabs.biz/blog/creating-episerver-forms-via-the-api/ 

#251435
Mar 25, 2021 21:55
* 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.