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); }
}
}