Skip to main content

Implement the 'required' check box field validator

Abstract

Make the check box field ‘required’ and create a custom validator for the check box field.

In the Web Forms for Marketers module, by default, the Check box field does not support the 'required' validation rule.

This topic outlines how you can make this field 'required' in the following ways:

  • Use the Check box field and mark it as required

  • Create a custom validator for the Check box field

Note

This is only valid for web forms and does not support MVC forms.

To make the Check box field 'required' by using the field:

  1. Add the Check box field to a web form.

  2. Add one item to the list.

  3. Mark this Check box as required.

  4. Click Save.

    097671ADBAC04628971213D9A01CA518.jpg

To create a custom validator for the Check box field:

  1. Create a class that is inherited from the System.Web.UI.WebControls.BaseValidator class. See the code sample:

    class CheckboxValidation : BaseValidator
        {
            protected CheckBox ctrToValidate;
            protected CheckBox CheckBoxToValidate
            {
                get
                {
                    if (ctrToValidate == null)
                    {
                        ctrToValidate = base.FindControl(ControlToValidate) as CheckBox;
                    }
                    return ctrToValidate;
                }
            }
            protected override bool ControlPropertiesValid()
            {
                if (base.ControlToValidate == null || base.ControlToValidate.Length == 0)
                {
                    throw new HttpException(string.Format("The ControlToValidate property of '{0}' cannot be blank.", this.ID));
                }
                if (this.CheckBoxToValidate == null)
                {
                    throw new HttpException(string.Format("The CheckBoxValidator can only validate controls of type CheckBox."));
                }
                return true;
            }
            protected override bool EvaluateIsValid()
            {
    this.ErrorMessage = string.Format(this.ErrorMessage, "{0}", CheckBoxToValidate.Text);
                //Validate whether checkbox is checked
                return this.CheckBoxToValidate.Checked == true;
            }
        }
    
  2. In the content tree, navigate to the folder sitecore/System/Modules/Web Forms for Marketers/Settings/Validation and create an item by clicking the BaseValidator button.

    The item must be based on the BaseValidator template.

  3. In the Message dialog, enter a name for the new item. Click OK.

  4. In the right pane, in the Assembly and Class fields, enter the relevant values from your custom assembly.

  5. In the Validator section, in the Error Message field, enter the following string: The {0} check box must be checked.

  6. In the Text field, enter a relevant message. If this field is blank, its value is the same as the one in the Error Message field.

  7. Duplicate the check box item (/sitecore/System/Modules/Web Forms for Marketers/Settings/Field Types/Simple Types/checkbox) and rename it to CheckBoxRequired.

    Note

    In the CheckBoxRequired item, do not select the Required check box.

  8. In the CheckBoxRequired item (/sitecore/system/modules/web forms for marketers/settings/field types/simple types/CheckBoxRequired), in the Validation field, select your custom validator.