0

I need to apply a regex based validation to see the number entered by content author is between 1 and 5 only. Problem is the validation should be applied to only specific content item only. ANy idea on how can we achieve this?

Note : I cannot add it on the field level as the template is shared across multiple items.

2

Let's start with your Note -

I cannot add it on the field level as the template is shared across multiple items.

You will need to create a separate field for this because a field validation can be applied to a field only, otherwise, you need to add some custom save action.

Now come to the second point, the answer provided by Yogesh is technically correct but when Sitecore gives you the OOTB feature you don't need to create any custom validation for this. Sitecore gives IntegerRangeValidator and RegexValidator, the examples of these types are -

  • IntegerRangeValidator - /sitecore/system/Settings/Validation Rules/Field Rules/Sample/Rating 1 to 9
  • RegexValidator - /sitecore/system/Settings/Validation Rules/Field Rules/Common/Is Email

You can use any of these for validating the field, all you need to copy one of them and update them accordingly. I am using IntegerRangeValidator here in this example -

enter image description here

Make sure to add type should be - Sitecore.Data.Validators.FieldValidators.IntegerRangeValidator,Sitecore.Kernel and parameter should contain Result=FatalError with min and max value- Min=1&Max=5&Result=FatalError.

Important - this validation is applicable for integer type of field so make sure your field type should be an integer

If you don't want to use integer type then you can use Sitecore.Data.Validators.FieldValidators.RegexValidator,Sitecore.Kernel as type and parameter - Pattern=YOURREGEX&Result=FatalError&Text=Field "{0}" is not a valid Range.

| improve this answer | |
0

I would suggest you to create custom validation for this. I recently post about the similar concept. https://sitecoretweaks.wordpress.com/2019/11/06/field-validation-on-specific-template-types/

In my case I am doind it for template speicfic but you can do it for item specific as per below code -

[Serializable]
public class CustomRequiredFieldValidator : StandardValidator
{
    /// <summary>
    /// Initializes a new instance of the  class.
    /// </summary>
    public CustomRequiredFieldValidator()
    {
    }

    /// <summary>
    /// Initializes a new instance of the  class.
    /// </summary>
    /// The Serialization info.
    /// The context.
    public CustomRequiredFieldValidator(SerializationInfo info, StreamingContext context) : base(info, context)
    {
    }

    /// <summary>
    /// Gets the name.
    /// </summary>
    /// The validator name.
    public override string Name
    {
        get
        {
            return "Required";
        }
    }

    /// <summary>
    /// When overridden in a derived class, this method contains the code to determine whether the value in the input control is valid.
    /// </summary>
    /// The result of the evaluation.
    protected override ValidatorResult Evaluate()
    {
        var itemID = MainUtil.GetStringList(true, this.Parameters["itemids"].Split('|'));
        if (itemID.Contains(this.GetItem().ID.ToString()))
        {
            if (!string.IsNullOrEmpty(this.ControlValidationValue))
            {
                return ValidatorResult.Valid;
            }

            this.Text = this.GetText("Field \"{0}\" must contain a value.", this.GetFieldDisplayName());
            return this.GetFailedResult(ValidatorResult.CriticalError);
        }

        return ValidatorResult.Valid;
    }

    /// <summary>
    /// Gets the max validator result.
    /// </summary>
    /// The max validator result.
    /// This is used when saving and the validator uses a thread. If the Max Validator Result
    /// is Error or below, the validator does not have to be evaluated before saving.
    /// If the Max Validator Result is CriticalError or FatalError, the validator must have
    /// been evaluated before saving.
    protected override ValidatorResult GetMaxValidatorResult()
    {
        return this.GetFailedResult(ValidatorResult.Error);
    }

If you don't want to give the feature for saving the item and don;t want user to Save the item until they correct it. Use Result=FatelError

Result=FatalError&itemids={0850B123-72B8-41B9-8695-0B440A83465H}|{0850B123-72B8-41B9-8695-0B440A837ED5}

So goo thing about this article is you can manage the item where you want to apply validation rules from Sitecore only. For example today you need this validation on some specific items but in future if requirement getting change you can manage those from Sitecore.

This is not exactly same for your question but with the small tweaks you can achieve the same.

| improve this answer | |
  • This will still allow the user to save the value. How can I prevent user from saving it. Thanks – Suhas Nov 11 '19 at 12:06
  • @Suhas - This is another question but you can try Result=FatalError in Parameter field Try this and see if it is working the question was about just add validation on specific items and if you will see the post you will be able to find the same. – Yogesh Sharma Nov 11 '19 at 12:29
  • @Suhas - I updated my answer now you can see what you need in second last paragraph. – Yogesh Sharma Nov 11 '19 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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