When within the context of a WFFM field custom validator, determining whether or not a field is required may not be a straight-forward task. Understand that within the context of the validator, there are two ways of obtaining an instance of a form:
- Obtain an instance of the current SimpleForm control, find your field and check if any MarkerLabels are present
- Obtain an instance of the WFFM form itself, find your field item and check if its Required field is checked
Approach 1: Obtain an Instance of the current SimpleForm
Here we’re getting an instance of the SimpleForm using
WebUtil.GetParent<SimpleForm>(). From there, we’re searching for our field, also via WebUtil. One very important thing to keep in mind is that the instance of the form we’re getting is the rendered output of the form itself and not the actual form item in Sitecore content. This makes it quite difficult to determine the checked state of the required checkbox on our form field. Instead, we’re dependent upon doing an
.Any() on the “Requred” property of our field to see if any
RequiredWithMarkerValidator items exist.
Shout out to Mike Reynolds for the idea of running
.Any() on the Requred property.
Approach 2: Obtain an Instance of the current WFFM Item
Here we’re still getting an instance of the
WebUtilGetParent<SimpleForm>(), but now we’re using the FormId property on that control to get the Sitecore ID of the form. We can then use this to get the item in Sitecore content to read the “Required” field value and determine its checked state.
Obviously, you’ll want to do additional null checking and most likely use an ORM like Synthesis to get strongly type objects mapping back to our WFFM forms and field items.