0

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); }
        }
    }
  • constructors are needed for serialization and you should not remove them. – Andrey Bobrov Feb 11 '17 at 11:49
2

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:

| improve this answer | |

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.