Tagged: Default value

Sitecore Forms : Custom Validation to Exclude the Default Value for Dropdown

Blog-Sitecore-Favorite-Tools (2).png   

I came across a scenario where I needed to exclude the default value for dropdowns in Sitecore Forms. By default, dropdown fields in Sitecore Forms often preselect the first option, which might be a placeholder like “Select an option.”

If users submit the form without changing this value, it can lead to unreliable data. Custom validation helps enforce user interaction, ensuring the selection is meaningful.

 

Steps to Implement Custom Validation in Sitecore Forms

 

Step 1: Create a Custom Validator

  • Navigate to your Sitecore solution and open the project where Sitecore Forms is configured.
  • Create a new class that extends FormConditionalValidator and override the validation logic.

 

 

Step 2: Register the Validator in Sitecore

  1. Open Sitecore Content Editor and navigate to:
    /sitecore/system/Settings/Forms/Validation
  2. Create a new validation item:
    • Name: Department Validator (Change it according to Project needs)
    • Type: Reference the class you created, e.g., Custom.Validators.DropdownDefaultValueValidator, CustomAssembly
    • Message: "{0} is required"
    • Parameters"{"fieldToCompare":{Guid-of-the-dropdown}", "valueToCompare": "Select"}
  3. Save and publish the changes.

Sitecore_Forms_Validation-1.png

 

Step 3: Apply the Validator to Your Dropdown Field

  1. Open your form in Sitecore Forms Designer.
  2. Select the Dropdown field where validation is needed.
  3. Under the Validation section, add the newly created validator.
  4. Save and publish the form.

Sitecore_Forms_Validation-2.png

Let’s test – It’s working! Yay!!

Hope this helps.

Happy Sitecoring!

 

0