Custom Field Validation With SPFieldValidationException
There was a post in the newsgroups wondering how when constructing a custom field type how one might build a small generic validation function in order to verify the harvested user input. There is provided the SPField.GetValidatedString method, which is overridable, that can be used in order to achieve this since it will take the object input provided by the custom field type and provide a workable string representation.
An example of this method is this:
- private readonly string m_strValidationException = "Hmmm, your string length wasen't 10!";
- public override string GetValidatedString(object oValue)
- if (oValue.ToString().Length != 10)
- throw new SPFieldValidationException(m_strValidationException);
- return oValue.ToString();