Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a fairly complex infopath 2010 browser form (Being used to process expense claims) that is using code to perform the submission rather than a rule. The initial submit uses code as it needs to do so using elevated privs, and further submissions need to submit back to the original library (one per user).The form is also set to close upon submission.

All works fine, unless any of the validation fails. In that case the user gets a validation error, as expected, but when they click to confirm, the form closes and they lose their work.

Can't find any easy way around this (Which seems like a common scenario to me)

Is there any way to call some sort of global validation event? In which case I could call for the fields to be validated before trying the submit, and abort if necessary.

If not, I guess I'll have to write all of the validation in code too? Couple of queries on the best way to go about that.

  • Is there a specific validation event for each control I could/should be using?

  • Or should I just write a generic 'Validate Rules' procedure that looks at each field in turn?

  • How can I display these validation messages back to the user in a browser form? Can I hook into the standard error dialog? Or will I have to just write back to some hidden form fields?

share|improve this question
add comment

2 Answers 2

No, there is no gobal validation event, you can only validate each field on its own (which you could make generic, but depends on requirements). You could however create a method that calls all field validation events before you submit the form.

To create a validation event for a field you simply select the field in InfoPath - click the "Developer" Ribbon - Validating Event and it automatically registers the event in your FormCode.

public void Value_Validating(object sender, XmlValidatingEventArgs e)
{
    if(e.Operation == XmlOperation.Insert || e.Operation == XmlOperation.ValueChange)
    {
        int myValue = Convert.ToInt32(e.NewValue);

        if(myValue > 100)
            e.ReportError(e.Site, false, "please enter a value less than 100");
    }
}

hope this helps

You don't actually need to call the validate-routines yourself, they fire automatically when a Validation event happens (i.e. when a user changes the value of a field).

I'd create a boolean private member in your code, that indicates whether the validation passed for the (infopath) field and before you submit, you check if all members are valid (true) like this:

private bool SomeInteger1Valid = false;
private bool SomeInteger2Valid = false;

public void BtnSubmitClick(object sender, ClickedEventArgs e)
{
    if (SomeInteger1Valid && SomeInteger2Valid )
    {
        //submit the form
    }
    else
    {
        //maybe show additional error message
    }
}

public void SomeInteger1_Validating(object sender, XmlValidatingEventArgs e)
{    
    if(Convert.ToInt32(e.NewValue) > 10)
        e.ReportError(e.Site, false, "error") 

    else SomeInteger1Valid = true;
}

public void SomeInteger2_Validating(object sender, XmlValidatingEventArgs e)
{    
    if (Convert.ToInt32(e.NewValue) < 10)
        e.ReportError(e.Site, false, "cannot be less than 10");

    else SomeInteger2Valid = true;
}
share|improve this answer
    
Andreas, Thanks, but I am still a little confused. how can i call each of the field validation routines? How do I pass in the relevant parameters to the validation routine for each field? What I was hoping for was some way to call all of the validation, with a return code indicating if any validations had failed, if so display the errors, if not than continue with the form submission. –  Carlovski Oct 19 '11 at 10:20
add comment

If you add a button to perform your own submit behaviour (using code), InfoPath will execute your code even if the form has validation errors. However, in most cases you'll want to make sure there are no validation errors before proceeding with the customized submit.

To ensure form validation succeeded, the following code can be added to the click event of your button:

    public void SubmitButton_Clicked(object sender, ClickedEventArgs e)
    {
        XmlForm form = sender as XmlForm;
        if (form.Errors.Count == 0)
        {
            // write your code here to process submit.
        }
    }

(The form.Errors collection contains one error object for each validation condition that has not yet been met in the form.)

share|improve this answer
    
Note: To improve user experience, I also create a custom field on my form called ErrorMessage and change its colour to red. Then, in the code above, I add an else clause that sets the ErrorMessage field, explaining to correct validation errors. –  Hutch May 28 '13 at 16:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.