<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.infopathdev.com:443/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Code</title><link>https://www.infopathdev.com:443/forums/45.aspx</link><description /><dc:language>en</dc:language><generator>CommunityServer 2007 SP3 (Build: 31118.962)</generator><item><title>Re: Validate required fields with Save Button</title><link>https://www.infopathdev.com:443/forums/thread/108597.aspx</link><pubDate>Tue, 15 Nov 2016 17:00:10 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:108597</guid><dc:creator>CRogado</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/108597.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=108597</wfw:commentRss><description>I did :) and worked :)

XmlForm form = sender as XmlForm;

 if (form.Errors.Count == 0 )
{
Submit the form
}
else

{ this.ViewInfos.SwitchView(&amp;quot;ValidationError&amp;quot;); 
}

Thanks for you help :)</description></item><item><title>Re: Validate required fields with Save Button</title><link>https://www.infopathdev.com:443/forums/thread/108590.aspx</link><pubDate>Tue, 15 Nov 2016 16:32:56 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:108590</guid><dc:creator>Hilary Stoupa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/108590.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=108590</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;I&amp;#39;m sorry, your reply confuses me. &lt;/p&gt;&lt;p&gt;I&amp;#39;m suggesting that at the very beginning of your save code, you check for errors, using code like that in the link:&lt;/p&gt;&lt;blockquote style="margin-right:0px;" dir="ltr"&gt;&lt;p&gt;int formErrors = this.Errors.Count;&lt;/p&gt;&lt;/blockquote&gt;&lt;p dir="ltr"&gt;Then you can just not run your save code if formErrors &amp;gt; 0.&lt;/p&gt;</description></item><item><title>Re: Validate required fields with Save Button</title><link>https://www.infopathdev.com:443/forums/thread/108463.aspx</link><pubDate>Sun, 06 Nov 2016 19:15:01 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:108463</guid><dc:creator>CRogado</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/108463.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=108463</wfw:commentRss><description>Thanks for your reply :) But can I do this? I need to create a field in &amp;quot;InfoPath&amp;quot; names &amp;quot;CountError&amp;quot; and declare a private class
 private string ErrorCount
        {
            get
            {
                return (String)FormState[&amp;quot;ErrorCount&amp;quot;];
            }
            set
            {
                FormState[&amp;quot;ErrorCount&amp;quot;] = value;
            }
        }

and in my function &amp;quot; public void btnSave_Clicked(object sender, ClickedEventArgs e)&amp;quot; what I need to do? Sorry, but Is my first time with InfoPath and C#.</description></item><item><title>Re: Validate required fields with Save Button</title><link>https://www.infopathdev.com:443/forums/thread/108381.aspx</link><pubDate>Mon, 31 Oct 2016 23:39:36 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:108381</guid><dc:creator>Hilary Stoupa</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/108381.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=108381</wfw:commentRss><description>&lt;p&gt;&amp;nbsp;&lt;a href="https://msdn.microsoft.com/en-us/library/office/microsoft.office.infopath.xmlform.errors.aspx"&gt;https://msdn.microsoft.com/en-us/library/office/microsoft.office.infopath.xmlform.errors.aspx&lt;/a&gt;&amp;nbsp;- you can get a count of the errors and then stop running your code, alert the user, etc.&lt;/p&gt;</description></item><item><title>Validate required fields with Save Button</title><link>https://www.infopathdev.com:443/forums/thread/108252.aspx</link><pubDate>Mon, 17 Oct 2016 08:08:24 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:108252</guid><dc:creator>CRogado</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/108252.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=45&amp;PostID=108252</wfw:commentRss><description>I have two buttons &amp;quot;Save and Submit&amp;quot;. The Save button, save the form in List without sending an e-mail to Approve. This form has 10 required fields. If I don&amp;#39;t fill this required fields when I click in save button the form doesn&amp;#39;t validate if the fields are fill or not.... 
I&amp;#39;m using rules and Custom Code:
 public void btnSave_Clicked(object sender, ClickedEventArgs e)
 {
               WriteEventLog(&amp;quot;Submitting the form&amp;quot;);
                // If the submit operation is successful, set
                //e.CancelableArgs.Cancel = false;
                // Write your code here.

                requestID = 0;
                filename = &amp;quot;&amp;quot;;
                requestStatus = &amp;quot;Draft&amp;quot;;

                // fetch the form name
                String formname = this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:formName&amp;quot;, this.NamespaceManager).Value;
                String currentYear = this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:EventDetails/my:requestYear&amp;quot;, this.NamespaceManager).Value;
                String shortName = this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:EventDetails/my:shortsubject&amp;quot;, this.NamespaceManager).Value;
                String eventDate = this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:EventDetails/my:event_date&amp;quot;, this.NamespaceManager).Value;
                String now = DateTime.Now.ToString();

                if (this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:fileName&amp;quot;, this.NamespaceManager) != null)
                {
                    filename = this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:fileName&amp;quot;, this.NamespaceManager).Value;
                }


                // Fetch the next ID 
                XPathNavigator idnavigator = this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:requestID&amp;quot;, this.NamespaceManager);


                if (idnavigator != null)
                {
                    requestID = fetchNextID(formname, currentYear);
                    idnavigator.SetValue(requestID.ToString());
                }

                filename = &amp;quot;Events--&amp;quot; + requestID.ToString(&amp;quot;000&amp;quot;);
                this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:fileName&amp;quot;, this.NamespaceManager).SetValue(filename);
                this.MainDataSource.CreateNavigator().SelectSingleNode(&amp;quot;/my:myFields/my:requestStatus&amp;quot;, this.NamespaceManager).SetValue(requestStatus);


                WriteEventLog(&amp;quot;All data is ready!&amp;quot;);
                try
                {
                    FileSubmitConnection fc = DataConnections[&amp;quot;Main submit&amp;quot;] as FileSubmitConnection;


                    fc.Execute();

                    WriteEventLog(&amp;quot;Form has been submitted&amp;quot;);


                }
                catch (Exception ex)
                {
                    WriteEventLog(ex.Message);
                }
            }

}

Rules:
Close the form -&amp;gt; When the button is clicked.

How can I do validate the required fields with the code? I cannot add the rule &amp;quot;Validation&amp;quot; in Button.</description></item></channel></rss>