1

I'm trying to set a validation rule for max.length on a text field. The following code works. For the related item, if the text entered is more than 100 chars., it shows the sitecore validation messages.

However, in the content editor, if I click on Validate button in the Review section, a dialog pops up with an error.

Server Error in '/' Application. The constructor to deserialize an object of type 'MySite.Validations.StringLengthValidator' was not found.

If, I uncomment the constructors, then on clicking validate, there is no error and it shows the validation dialog. But the max. length validation does not apply at all. No validation messages when the text > 100.

Using Sitecore 8.0 with MVC

[Serializable]
    public class StringLengthValidator : StandardValidator
    {
        //public StringLengthValidator()
        //{

        //}

        //public StringLengthValidator(SerializationInfo info, StreamingContext context) : base(info, context)
        //{

        //}

        public const Int32 MaxStringLength = 100; // Change me

        protected override ValidatorResult Evaluate()
        {
            if (!String.IsNullOrEmpty(base.ControlValidationValue))
            {
                if (base.ControlValidationValue.Length > MaxStringLength)
                {
                    base.Text = base.GetText("The field '{0}' exceeds the maximum string length of {1}", new String[]{
                        base.GetFieldDisplayName(),
                        MaxStringLength.ToString()
                    });

                    return base.GetFailedResult(ValidatorResult.Error);
                }
            }

            return ValidatorResult.Valid;
        }

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

        public override String Name
        {
            get { return String.Format("Must be {0} or fewer characters", MaxStringLength); }
        }
    }
1
  • constructors are needed for serialization and you should not remove them. Commented Feb 11, 2017 at 11:49

2 Answers 2

3

As mentioned in the comments, the constructors are mandatory. You are inheriting from StandardValidator which ultimately inherits from a class implementing ISerializable. On this we know:

The ISerializable interface implies a constructor with the signature constructor (SerializationInfo information, StreamingContext context). At deserialization time, the current constructor is called only after the data in the SerializationInfo has been deserialized by the formatter. In general, this constructor should be protected if the class is not sealed.

Source: ISerializable Interface

At a glance, the rest of your implementation looks ok. Which then must mean, the likely reason you are not getting the validation errors you are expecting is in the wiring. For any ideas on that, you would need to share your configuration setup of this validator.

For reference:

0
0

First, make sure that the error in question is related to the validator you're expecting. If you have multiple custom validators / validations, such an error can trigger on any one of them if they aren't set up correctly.

Ensure that all of your custom validators have the [Serializable] attribute on the class as well as the following two empty constructors:

using System.Runtime.Serialization;

...

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

public MyCustomValidation() { }

If after making the changes you find that your validators no longer get called, check your logs for warnings such as:

Could not find constructor in ReflectionUtil.CreateObject: Client.Project.Web.Validation.ItemRules.Links.MyCustomValidation. The constructor parameters may not match or it may be an abstract class. Parameter info: Count: 0

This may occur when inexperienced developers add validation rules to the wrong items or add them in multiple locations superfluously. Imagine a scenario where a custom validation rule gets added to a template item instead of, or in addition to, the Standard Values and/or individual field items. Point is, if you are still having issues after adding the constructors, start from scratch and verify all the places in which the validation rule is referenced.

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.