A Practical Guide to SharePoint 2013

A Practical Guide to SharePoint 2013
A Practical Guide to SharePoint 2013 - Book by Saifullah Shafiq

Wednesday, July 9, 2008

InfoPath: Raising an error on validation

InfoPath provides excellent method to validate controls, for example, you can use data validation alone or in conjuction with Rules to validate data in InfoPath forms. You can also do it  programmatically and raise errors which will be shown in the form when there is a validation error. Assume there is a control called FirstName. Here is how you will raise an error on FirstName:
public void FirstName_Changed(object sender, XmlEventArgs e)
{

}
Right click the control and select Programming > Changed Event. Note, Validating Event is also available but we will use Changed Event. Raise any previously raised errors:
this.Errors.Delete("FirstNameError");
After validating, if there is an error, raise it:
 this.Errors.Add(Nav, "FirstNameError", "Please add correct value in First Name");
The first parameter is the XPathNavigator object that points to the control.
XPathNavigator nav = this.CreateNavigator().SelectSingleNode(XPath of the firstname field, this.NamespaceManager);
XPath will be like "/my:myFields/my:FirstName".
The second parameter is the name of the error and third parameter is the description of the error.
Happy programming!