{"id":560,"date":"2004-01-23T12:33:03","date_gmt":"2004-01-23T16:33:03","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=560"},"modified":"2004-01-23T12:33:03","modified_gmt":"2004-01-23T16:33:03","slug":"struts-actionform-validate-question","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/01\/23\/struts-actionform-validate-question\/","title":{"rendered":"Struts ActionForm validate() question"},"content":{"rendered":"<p>I&#8217;m trying to setup a simple contact us form that gathers information from a user, checks that the information is valid and then emails the information off to a third party using <a href=\"http:\/\/jakarta.apache.org\/struts\/\">Struts<\/a> v1.1, specifically an <a href=\"http:\/\/jakarta.apache.org\/struts\/api\/org\/apache\/struts\/action\/ActionForm.html\">ActionForm<\/a> and an <a href=\"http:\/\/jakarta.apache.org\/struts\/api\/org\/apache\/struts\/action\/Action.html\">Action<\/a>. In my struts-config.xml, under &lt;action-mappings&gt;, I have this:<br \/>\n<code><br \/>\n&lt;action<br \/>\n&nbsp;&nbsp;path=\"\/contact\/requestinfo\"<br \/>\n&nbsp;&nbsp;type=\"com.mycompany.actions.RequestInfoAction\"<br \/>\n&nbsp;&nbsp;name=\"requestInfoForm\"<br \/>\n&nbsp;&nbsp;scope=\"request\"<br \/>\n&nbsp;&nbsp;validate=\"true\"<br \/>\n&nbsp;&nbsp;input=\"\/views\/requestinfo.jsp\"&gt;<br \/>\n&nbsp;&nbsp;&lt;forward name=\"success\" path=\"\/views\/requestinfo_confirm.jsp\"\/&gt;<br \/>\n&lt;\/action&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;<br \/>\n<\/code><br \/>\nand then obviously a &lt;form-bean&gt; definition above that.  What I expected to happen was that the initial request would be forwared to the page specified by the input attribute, the user would then submit the form, the validate() method would be called on the ActionForm instance, and then if the validate() method return no <a href=\"http:\/\/jakarta.apache.org\/struts\/api\/org\/apache\/struts\/action\/ActionErrors.html\">ActionErrors<\/a>, the form would be passed on to the Action. What happens instead is that the validate() method is called on the ActionForm before the user even submits the form, which causes the error messages (&#8220;First name is a required field.&#8221;) to be displayed before they have even submitted the form.  <\/p>\n<p>In the short-term, the workaround is to define an action mapping that simply forwards the user to the input page and then a second mapping that has the validate=true attribute set, so the page flow looks like this:<\/p>\n<p>\/contact\/requestinfo &#8211;&gt; display form &#8211;&gt; submit form &#8211;&gt; \/contact\/requestinfoform &#8211;&gt; validate() &#8211;&gt; if no errors, then servlet forwards to the Action<\/p>\n<p>Is there a way to have to validate() method not called on the intial load of the page? Any thoughts?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m trying to setup a simple contact us form that gathers information from a user, checks that the information is valid and then emails the information off to a third party using Struts v1.1, specifically an ActionForm and an Action. In my struts-config.xml, under &lt;action-mappings&gt;, I have this: &lt;action &nbsp;&nbsp;path=&#8221;\/contact\/requestinfo&#8221; &nbsp;&nbsp;type=&#8221;com.mycompany.actions.RequestInfoAction&#8221; &nbsp;&nbsp;name=&#8221;requestInfoForm&#8221; &nbsp;&nbsp;scope=&#8221;request&#8221; &nbsp;&nbsp;validate=&#8221;true&#8221; &nbsp;&nbsp;input=&#8221;\/views\/requestinfo.jsp&#8221;&gt; &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/01\/23\/struts-actionform-validate-question\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Struts ActionForm validate() question<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[3],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/560"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=560"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/560\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}