0

We are working on Sitecore 10.3. We have a multilist field and need to add validation to this field to accept a minimum of 1 and a maximum of 3 items. We have tried using the RegexValidator with the following two options:

  1. FatalError: The item is not saving, and the red indicator is not appearing near the field.
  2. CriticalError: The item is saving with a red indicator.

However, the requirement is for the item not to be saved and to show the red indicator near the field.

Validation rule screenshot screenshot of validation rule

Could anyone please assist with writing these validation rules

Thanks.

4 Answers 4

1

By default, FatalError will also show you the red indicator as shown below where in 'Countries' field is restricted from entering 1 to 3 countries.

enter image description here

Since setting result to FatalError in the Validation Rules, prevents you to save the item unless the condition is satisfied, thats why red indicator is not visible. But if the limit set inside the Validation Rule is not adhered, red indicator will be visible as expected.

Hope this helps!!!

3
  • What validation rule have you applied to the "countries" field?
    – Saiprasad
    Commented May 3 at 4:26
  • @Saiprasad I have added the validation rule as "^({[^}]+}(\|){0,1}){1,3}$". This make sure you can select min 1 and max 3 countries. Complete paramters looks like this Pattern=^.{38,116}$&Text=Please ensure you can select a minimum of one "{0}" and maximum of 3 "{0}"&Result=FatalError.. Hope this helps!!!
    – ckhanna
    Commented May 3 at 9:17
  • Sorry !! Red color not showing the near the field. only showing popup to restrict the items. but i need to show the red color before saving the item.
    – Saiprasad
    Commented May 6 at 5:57
1

you can add validation in the field to achieve this. In your data template, there are fields for validation and validation text. There, you can use this pattern: "^({[^}]+}|?){1,3}$". This will not allow you to save until this pattern not matched Hope this helps!

Validation

For more detail you can refer this blog : Field validation in Sitecore

1
  • Yes i followed same, but my requirement is to show red color near the field also.
    – Saiprasad
    Commented May 3 at 4:23
0

To create a validation rule for multilist that shows a red color near the field you can follow these steps. Here I am taking the example to limit with three items.

  1. Create a validation rule of type RegexValidator.

  2. In the Parameters field add this value.

    Pattern=^.{38,116}$&Text=Field "{0}" required three items.
    

enter image description here

  1. Select this validation rule in your field in the Validator Bar.

enter image description here

  1. Now if you select more than three items in your multilist, you will see the red error bar.

enter image description here

To change your validation rule regex, follow this thread.

https://community.sitecore.com/community/en?id=community_blog&sys_id=24e263211bc370d0b8954371b24bcb0b

Hope this helps.

1
  • Thanks for replying. red color is showing but i need to show before saving the item.
    – Saiprasad
    Commented May 6 at 5:40
0

If you want to achieve this scenario, you can create your own custom validator.

Step1: Create a custom validator in the solution

using Sitecore.Data.Validators;
using Sitecore.Text;
using Sitecore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;
using Sitecore.Data.Fields;

namespace MySolution.Validators.FieldValidators
{
    [Serializable]
    public class NumberOfSelectedItemsValidator : StandardValidator
    {
        public NumberOfSelectedItemsValidator()
        {
        }

        public NumberOfSelectedItemsValidator(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }

        public override string Name
        {
            get { return "Number of Selected Items"; }
        }

        protected override ValidatorResult GetMaxValidatorResult()
        {
            return GetFailedResult(ValidatorResult.FatalError);
        }

        protected override ValidatorResult Evaluate()
        {
            int minNumberOfItems = MainUtil.GetInt(Parameters["min"], 0);
            int maxNumberOfItems = MainUtil.GetInt(Parameters["max"], 3);

            Field fieldToValidate = GetField();
            if (fieldToValidate == null)
            {
                // if nothing to validate then it's valid.
                Text = string.Empty;
                return ValidatorResult.Valid;
            }

            ListString list = new ListString(ControlValidationValue);
            if (list.Count >= minNumberOfItems && list.Count <= maxNumberOfItems)
            {
                Text = string.Empty;
                return ValidatorResult.Valid;
            }

            Text = $"The field {fieldToValidate.Title} must have between {minNumberOfItems} and {maxNumberOfItems} items selected.";
            return GetMaxValidatorResult();
        }
    }
}

Step2: Create a field rule in sitecore under /sitecore/system/Settings/Validation Rules/Field Rules

Step 3: Refer your DLL and pass the min and max parameter

enter image description here

Step 4: Add this validator in your template fields.

This will ensure you will see the red bar on item load.

Hope this helps!!

3
  • Thank you !! This rule is displaying an error message in the validation results popup, but I want to show the red color near the field before saving the item.
    – Saiprasad
    Commented May 6 at 5:55
  • This will show the red color on item create when the filed is empty and not allow you to save until the perfect condition is met. If you have saved the item and then edit the item with conditions violating it will not allow you to save Commented May 6 at 11:51
  • It's a sitecore bug so we have raised request to sitecore team.
    – Saiprasad
    Commented May 20 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.