Problem: You want to be able to submit an InfoPath form despite any data validation errors that the form may have, so you want to ignore and delete all validation errors before submitting the form.
You can use the DeleteAll() method of the Errors collection to clear all of the errors in a form:
To Ignore and clear all of the errors in a form before submitting the form:
1 - Create a new form template
2 - Add a Textbox control to the view nad make it a required field by selecting its Cannot Be Blank Property.
3 - Go to the Submit Option under File and select "Allow users to submit this form" check box, select the "Perform custom action code option, deselect the "Show the Submit button in both the ribbon and the info tab in the InfoPath Filler". Click on Edit Code.
4 - Add following code to the FormEvents_Submit() event handler
Public Sub FormEvents_Submit(ByVal sender As Object, ByVal e As SubmitEventArgs)
' If the submit operation is successful, set
' e.CancelableArgs.Cancel = False
' Write your code here.
System.IO.File.WriteAllText("C:\Temp\myform.xml", MainDataSource.CreateNavigator().OuterXml)
e.CancelableArgs.Cancel = False
End Sub
You will need to give the form full trust in order to save it to disk
5 - Add a button control to the view and label it "Submit". Add the following code to the clicked event handler of this button
Public Sub CTRL2_5_Clicked(ByVal sender As Object, ByVal e As ClickedEventArgs)
' Write your code here.
If Me.Errors.Count > 0 Then
Me.Errors.DeleteAll()
End If
Submit()
End Sub
* The first three lines of code delete all of the errors that are in the Errors collection of the form, and the fourth line of code calls the FormEvents_Submit() event handler to submit the form.
6 - Save and build project
7 - Preview the form.
When the form opens, a red asterisk (*) should appear in the text box as an indication that it is a require field. Click the button. The form should be submitted without warning you about any data validation errors.
The purpose of this blog is to show techniques involving SharePoint development which is a platform I'm really passionate about. Some of techniques will include InfoPath, Nintex, Flow, REST API, CSOM/JSOM, PowerBI, etc...
Showing posts with label Data Validation. Show all posts
Showing posts with label Data Validation. Show all posts
Saturday, January 26, 2013
Thursday, November 22, 2012
Clear a specific validation error in a form
Problem: You want to be able to clear a specific error in a form.
You can use the Delete() method of the Errors collection to clear a specific error in a form by passing either a name or a FormError object to the method.
To clear a specific error in a form by name:
1 - Create a new form template and add a Text Box control. Name the control field1
2 - Add the following code to the event handler for the Validating event of the text box field 1
If e.Site IsNot Nothing Then
If e.Site.Value.Length > 10 Then
Errors.Add(e.Site, "MaxCharsErr", "Only 10 characters max allowed.")
End If
End If
This code checks whether the amount of characters typed into the text box exceeds 10 and if it does it adds a user-defined error named MaxCharsErr to the Errors collection of the form.
3 - Add a second Text Box control to the form and call it "Required". Make this field required by selecting its Cannot be Blank property. The purpose of this text box is to add a schema validation error to the Errors collection of the form, so that there is more than one error in the collection for testing purposes.
3 - Add a Button to the form. Add the following code to the Clicked event of the button
Dim errors As FormError() = Me.Errors.GetErrors("MaxCharsErr")
If errors IsNot Nothing Then
If errors.Length > 0 Then
Me.Errors.Delete("MaxCharsErr")
End If
End If
The first line of code retrieves all of the errors that have the name MaxCharsErr from the Errors collection. The second and third lines of code check whether any FormError objects were returned by the GetErrors() method, and if so, the Delete() method is called to delete them.
4 - Save and Preview the form
When the form opens, a red asterisk (*) should appear in the required text box as an indication that it is a required field. Enter a piece of text that is longer than 10 characters in the field1 text box. A red dashed border should appear around the text box. Click the button and the red dashed border should disappear but the asterisks in the required text box should still be present. With this you have cleared the validation error on the field1 text box, not not on the required text box.
You can use the Delete() method of the Errors collection to clear a specific error in a form by passing either a name or a FormError object to the method.
To clear a specific error in a form by name:
1 - Create a new form template and add a Text Box control. Name the control field1
2 - Add the following code to the event handler for the Validating event of the text box field 1
If e.Site IsNot Nothing Then
If e.Site.Value.Length > 10 Then
Errors.Add(e.Site, "MaxCharsErr", "Only 10 characters max allowed.")
End If
End If
This code checks whether the amount of characters typed into the text box exceeds 10 and if it does it adds a user-defined error named MaxCharsErr to the Errors collection of the form.
3 - Add a second Text Box control to the form and call it "Required". Make this field required by selecting its Cannot be Blank property. The purpose of this text box is to add a schema validation error to the Errors collection of the form, so that there is more than one error in the collection for testing purposes.
3 - Add a Button to the form. Add the following code to the Clicked event of the button
Dim errors As FormError() = Me.Errors.GetErrors("MaxCharsErr")
If errors IsNot Nothing Then
If errors.Length > 0 Then
Me.Errors.Delete("MaxCharsErr")
End If
End If
The first line of code retrieves all of the errors that have the name MaxCharsErr from the Errors collection. The second and third lines of code check whether any FormError objects were returned by the GetErrors() method, and if so, the Delete() method is called to delete them.
4 - Save and Preview the form
When the form opens, a red asterisk (*) should appear in the required text box as an indication that it is a required field. Enter a piece of text that is longer than 10 characters in the field1 text box. A red dashed border should appear around the text box. Click the button and the red dashed border should disappear but the asterisks in the required text box should still be present. With this you have cleared the validation error on the field1 text box, not not on the required text box.
Saturday, October 27, 2012
Check for a specific error in a form (Error Summary)
Problem: You have a text box control on an InfoPath form and want to display a list of errors that have been raised for the data entered into the text box.
You can use the Errors property of a form to retrieve error messages for a field.
1 - Create a new form template and add two Text Boxes control to the page and name it "field2" and "Errors".
2 - Select the Text Box and select its Cannot Be Blank property.
3 - Add two Button control to the form and label them "Set User-Defined Errors" and "Get All Errors".
4 - For the Clicked() event handler of the "Set User-Defined Errors" button add:
Dim field2 As XPathNavigator = MainDataSource.CreateNavigator.SelectSingleNode("/my:myFields/my:field2", NamespaceManager)
Errors.Add(field2, "Error1", "This is error 1.")
Errors.Add(field2, "Error2", "This is error 2.")
Errors.Add(field2, "Error3", "This is error 3.")
This code should raise three user-defined errors on the text box.
5 - For the Clicked() event handler of the "Get All Errors" button add:
Dim sb As New System.Text.StringBuilder
For Each err As FormError In Errors
sb.AppendFormat("Form Error Type: {0} - ", err.FormErrorType)
sb.AppendFormat("Message: {0}", err.Message)
sb.AppendLine()
Next
MainDataSource.CreateNavigator.SelectSingleNode("/my:myFields/my:Errors", NamespaceManager).SetValue(sb.ToString)
6 - Save and preview the form
When the form opens click the "Get All Errors" button and you should see error messages appear in the errors text box. After that click on the "Set User-Defined Errors" button and then click on the "Get All Errors" button again. You will that the three user-defined errors should appear in the errors text box.
ps. Use "err.Site.LocalName" if you would like to retrieve the name of field on which the error occurred.
The following code:
sb.AppendFormat("The field " & err.Site.LocalName & " has the following error: " & err.Message)
Renders:
The field salary has the following error: Cannot be blank
You can use the Errors property of a form to retrieve error messages for a field.
1 - Create a new form template and add two Text Boxes control to the page and name it "field2" and "Errors".
2 - Select the Text Box and select its Cannot Be Blank property.
3 - Add two Button control to the form and label them "Set User-Defined Errors" and "Get All Errors".
4 - For the Clicked() event handler of the "Set User-Defined Errors" button add:
Dim field2 As XPathNavigator = MainDataSource.CreateNavigator.SelectSingleNode("/my:myFields/my:field2", NamespaceManager)
Errors.Add(field2, "Error1", "This is error 1.")
Errors.Add(field2, "Error2", "This is error 2.")
Errors.Add(field2, "Error3", "This is error 3.")
This code should raise three user-defined errors on the text box.
5 - For the Clicked() event handler of the "Get All Errors" button add:
Dim sb As New System.Text.StringBuilder
For Each err As FormError In Errors
sb.AppendFormat("Form Error Type: {0} - ", err.FormErrorType)
sb.AppendFormat("Message: {0}", err.Message)
sb.AppendLine()
Next
MainDataSource.CreateNavigator.SelectSingleNode("/my:myFields/my:Errors", NamespaceManager).SetValue(sb.ToString)
6 - Save and preview the form
When the form opens click the "Get All Errors" button and you should see error messages appear in the errors text box. After that click on the "Set User-Defined Errors" button and then click on the "Get All Errors" button again. You will that the three user-defined errors should appear in the errors text box.
ps. Use "err.Site.LocalName" if you would like to retrieve the name of field on which the error occurred.
The following code:
sb.AppendFormat("The field " & err.Site.LocalName & " has the following error: " & err.Message)
Renders:
The field salary has the following error: Cannot be blank
Wednesday, October 17, 2012
Validate a field when its value changes
Problem: You have a text box control on an InfoPath form and want to display an error to the user if the text the user types into the text box is longer than 10 characters.
You can use the ReportError() method of the XmlValidatingEventArgs object or the Errors property of the form to display an error message to the user validating a field.
To validate a field when its value changes:
1 - Create a new form template and a Text Box control to it
2 - Add an event handler for the Validating event of the text box control
3 - If you want to use the ReportError() method to display the error message add the following code below:
Dim fieldVal As String = e.Site.Value
If Not String.IsNullOrEmpty(fieldVal) Then
If fieldVal.Length > 10 Then
e.ReportError(e.Site, False, "Only 10 characters max allowed.")
End If
End If
or if you want to use the Errors() property of the form to display an error message:
4 - Save and preview the form
While the behavior of the two methods for displaying an error message to a user are similar, they differ in that the ReportError() method adds a SystemGenerated error to the FormErrorCollection object of the form, while the Add() method adds a UserDefined error to the FormErrorCollection object of the form.
You can use the ReportError() method of the XmlValidatingEventArgs object or the Errors property of the form to display an error message to the user validating a field.
To validate a field when its value changes:
1 - Create a new form template and a Text Box control to it
2 - Add an event handler for the Validating event of the text box control
3 - If you want to use the ReportError() method to display the error message add the following code below:
Dim fieldVal As String = e.Site.Value
If Not String.IsNullOrEmpty(fieldVal) Then
If fieldVal.Length > 10 Then
e.ReportError(e.Site, False, "Only 10 characters max allowed.")
End If
End If
or if you want to use the Errors() property of the form to display an error message:
Dim err As FormError() = Me.Errors.GetErrors("MaxCharError")
If err IsNot Nothing Then
If err.Length > 0 Then
Me.Errors.Delete("MaxCharError")
End If
End If
Dim fieldVal As String = e.Site.Value
If Not String.IsNullOrEmpty(fieldVal) Then
If fieldVal.Length > 10 Then
Me.Errors.Add(e.Site, "MaxCharError", "Only 10 characters max allowed.")
End If
End If
While the behavior of the two methods for displaying an error message to a user are similar, they differ in that the ReportError() method adds a SystemGenerated error to the FormErrorCollection object of the form, while the Add() method adds a UserDefined error to the FormErrorCollection object of the form.
Subscribe to:
Posts (Atom)
