November Happy Hour will be moved to Thursday December 5th.

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?

#249969
Mar 12, 2021 14:46
Vote:
 

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"
#250991
Mar 19, 2021 9:58
* 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.