Validate required fields with Save Button - InfoPath Dev
in

InfoPath Dev

Use our Google Custom Search for best site search results.

Validate required fields with Save Button

Last post 11-15-2016 09:00 AM by CRogado. 4 replies.
Page 1 of 1 (5 items)
Sort Posts: Previous Next
  • 10-17-2016 01:08 AM

    Validate required fields with Save Button

    I have two buttons "Save and Submit". The Save button, save the form in List without sending an e-mail to Approve. This form has 10 required fields. If I don't fill this required fields when I click in save button the form doesn't validate if the fields are fill or not.... I'm using rules and Custom Code: public void btnSave_Clicked(object sender, ClickedEventArgs e) { WriteEventLog("Submitting the form"); // If the submit operation is successful, set //e.CancelableArgs.Cancel = false; // Write your code here. requestID = 0; filename = ""; requestStatus = "Draft"; // fetch the form name String formname = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:formName", this.NamespaceManager).Value; String currentYear = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:EventDetails/my:requestYear", this.NamespaceManager).Value; String shortName = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:EventDetails/my:shortsubject", this.NamespaceManager).Value; String eventDate = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:EventDetails/my:event_date", this.NamespaceManager).Value; String now = DateTime.Now.ToString(); if (this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:fileName", this.NamespaceManager) != null) { filename = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:fileName", this.NamespaceManager).Value; } // Fetch the next ID XPathNavigator idnavigator = this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:requestID", this.NamespaceManager); if (idnavigator != null) { requestID = fetchNextID(formname, currentYear); idnavigator.SetValue(requestID.ToString()); } filename = "Events--" + requestID.ToString("000"); this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:fileName", this.NamespaceManager).SetValue(filename); this.MainDataSource.CreateNavigator().SelectSingleNode("/my:myFields/my:requestStatus", this.NamespaceManager).SetValue(requestStatus); WriteEventLog("All data is ready!"); try { FileSubmitConnection fc = DataConnections["Main submit"] as FileSubmitConnection; fc.Execute(); WriteEventLog("Form has been submitted"); } catch (Exception ex) { WriteEventLog(ex.Message); } } } Rules: Close the form -> When the button is clicked. How can I do validate the required fields with the code? I cannot add the rule "Validation" in Button.
  • 10-31-2016 04:39 PM In reply to

    Re: Validate required fields with Save Button

     https://msdn.microsoft.com/en-us/library/office/microsoft.office.infopath.xmlform.errors.aspx - you can get a count of the errors and then stop running your code, alert the user, etc.

    Hilary Stoupa

  • 11-06-2016 11:15 AM In reply to

    Re: Validate required fields with Save Button

    Thanks for your reply :) But can I do this? I need to create a field in "InfoPath" names "CountError" and declare a private class private string ErrorCount { get { return (String)FormState["ErrorCount"]; } set { FormState["ErrorCount"] = value; } } and in my function " public void btnSave_Clicked(object sender, ClickedEventArgs e)" what I need to do? Sorry, but Is my first time with InfoPath and C#.
  • 11-15-2016 08:32 AM In reply to

    Re: Validate required fields with Save Button

     I'm sorry, your reply confuses me.

    I'm suggesting that at the very beginning of your save code, you check for errors, using code like that in the link:

    int formErrors = this.Errors.Count;

    Then you can just not run your save code if formErrors > 0.

    Hilary Stoupa

  • 11-15-2016 09:00 AM In reply to

    Re: Validate required fields with Save Button

    I did :) and worked :) XmlForm form = sender as XmlForm; if (form.Errors.Count == 0 ) { Submit the form } else { this.ViewInfos.SwitchView("ValidationError"); } Thanks for you help :)
Page 1 of 1 (5 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.