Tagged: custom validation

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

Sitecore 9 Forms: Custom Regex Validation

I came across a scenario where i implemented a custom validation on Sitecore 9 Forms. Sharing my knowledge here. You can achieve this in three simple steps.

Step 1: Create custom validator

Create a custom validator under
/sitecore/system/settings/forms/validation based on Validation(sitecore/templates/System/Forms/Validation) template like below

Step 2: Fill out the fields

Fill out your own regex and error message. You can copy the Type value from other validator.

Step 3: Assign the custom validator

You can now add the new custom validator to any Form fields(
/sitecore/system/Settings/Forms/Field Types). All set and no code required!

Hope you found this blog helpful. Any questions, please leave a comment.

Happy Sitecoring!

0