You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Binil Thomas <bi...@gmail.com> on 2007/03/12 16:37:14 UTC

Getting the AJAX Form Validations to work

Hi all,

I am trying out the Ajax Form validations in S2. I see the validations
example working perfectly fine in Showcase demo, but I can't get it to work
on my machine.

Here is what I did:

1) Created a maven project using the provided archetype.
2) Edited the struts.xml file to add my action:
<struts>
        ....
	<package name="twms" extends="struts-default">
		<action name="show" class="validations.TryValidationAction" method="show"> 
			<result>index.jsp</result>
			<result name="input">index.jsp</result>
		</action>		
	</package>
</struts>
3) Created src/main/java/validations/TryValidationAction.java as:
package validations;
import com.opensymphony.xwork2.ActionSupport;
public class TryValidationAction extends ActionSupport {
    public String show() {
        System.out.println("Show: Name = " + name + ", age = " + age);
        return SUCCESS;
    }
    
    private String name;
    private int age;
    // .. getters and setters for the two properties
}
4) Created a
src/main/resources/validations/TryValidationAction-show-validations.xml file
as:
<validators>
	<field name="name">
		<field-validator type="requiredstring">
			<param name="trim">true</param>
			<message>Name cannot be empty</message>
		</field-validator>
	</field>
</validators>
5) Created src/main/webapp/twms/index.jsp as:
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<title>Testing Validations</title>
<s:head theme="xhtml"/>
</head>
<body>
<s:form action="show.action" theme="xhtml">
	<s:textfield label="Name" name="name"/>
	<s:textfield label="Age" name="age"/>
	<s:submit value="Submit"/>
</s:form>
</body>
</html>
6) Configured decorators.xml to exclude my action.

With this set up in place, I was able to load the form, and see the
validations working when it is submitted. But when I change the theme to
Ajax (in the head and the form), I get plenty of Dojo errors in FireBug, I
don't get any validation errors and I am not able to submit the form.

I am using version 2.0.3 of struts, as was set up by the archetype. Can
someone help me get this working?

Thanks,
Binil
PS: I can send my simple project setup, if anyone is interested.
-- 
View this message in context: http://www.nabble.com/Getting-the-AJAX-Form-Validations-to-work-tf3390223.html#a9436888
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [S2] Getting the AJAX Form Validations to work

Posted by Binil Thomas <bi...@gmail.com>.
Hi all,

I got it working!

I had to add a validate="true" attribute on the <s:form/>. With that added,
the form was trying to make an AJAX request onblur. But I got a JS error in
FB:
    element.form.attributes['name'] is undefined.
Debugging a bit, I figured that the JS figures out the validation action to
invoke on the server from the name attribute of the form. I gave the form a
name (name="myform"). Now the AJAX request was made, but DWR was throwing up
some errors on the server-side.
    SEVERE: Error while trying to validate
    There is no Action mapped for action name myform. - [unknown location]
True, there is no action with the name 'myform'. I changed the name of the
form to the action name as it all started working as expected!
    <s:form method="post" theme="ajax" validate="true" name="show">
        <s:textfield label="Name" name="name"/>
        <s:textfield label="Age" name="age"/>
        <s:submit value="Submit"/>
    </s:form>

I copied the evaluated JSP into an HTML page, and debugged it. I was able to
remove all JS errors in FB by:
    1) Commenting out a <script language="JavaScript" type="text/javascript"
src="/validations/struts/simple/dojoRequire.js"></script> added by
the<s:head theme="ajax"/> tag.
    2) Commenting out a dojo.require("dojo.widget.html.Tooltip") line added
by, I think, the <s:form/> tag. I think Dojo guys have renamed the widget to
dojo.widget.Tooltip since 0.3.

With these changes, I was able to remove every JS error in FB. I now get
some Dojo deprection warnings though.
    DEBUG: DEPRECATED: dojo.style replaced by dojo.html.style -- will be
removed in version: 0.5
    DEBUG: DEPRECATED: dojo.animation.AnimationEvent is slated for removal
in 0.5; use dojo.lfx.* instead. 0.5
    DEBUG: DEPRECATED: dojo.animation.Animation is slated for removal in
0.5; use dojo.lfx.* instead. 0.5
    DEBUG: DEPRECATED: dojo.animation.AnimationSequence is slated for
removal in 0.5; use dojo.lfx.* instead. 0.5
    DEBUG: DEPRECATED: dojo.Animation.* is slated for removal in 0.5; use
dojo.lfx.* instead. 0.5
    DEBUG: DEPRECATED: dojo.graphics.color.Color is now
dojo.gfx.color.Color. 0.5

Thanks,
Binil


Binil Thomas wrote:
> 
> Hi Musachy,
> 
> Thanks for replying!
> 
> On FB I see HTTP 404s on:
> 1) http://localhost:8080/validations/stuts/dojo/src/widget/html/Tooltip.js
> 2) http://localhost:8080/validations/stuts/dojo/src/widget/html.js
> 3) http://localhost:8080/validations/stuts/dojo/src/widget.js
> 4) http://localhost:8080/validations/stuts/dojo/src/__package__.js
> 5) http://localhost:8080/validations/stuts/dojo/src.js
> 6) http://localhost:8080/validations/stuts/dojo/__package__.js
> 
> Also, on both FB and the page, I see the error:
> Could not load 'dojo.widget.html.Tooltip'; last tried '__package__.js'
> (I have enabled Dojo debug now). I think the Dojo widget is called
> dojo.widget.Tooltip.
> 
> When the form first shows up I see the field error message for 'name'
> field. When I fill out the field and tab out of it, no AJAX request is
> send to the server (I am assuming that the expectation is that the form
> gets validated at the server via an AJAX call onBlur).
> 
> When I click submit, an AJAX request is send and the server responds with
> the HTML page for the action; but nothing happens to the page.
> 

-- 
View this message in context: http://www.nabble.com/Getting-the-AJAX-Form-Validations-to-work-tf3390223.html#a9449588
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: [S2] Getting the AJAX Form Validations to work

Posted by Binil Thomas <bi...@gmail.com>.
Hi Musachy,

Thanks for replying!

On FB I see HTTP 404s on:
1) http://localhost:8080/validations/stuts/dojo/src/widget/html/Tooltip.js
2) http://localhost:8080/validations/stuts/dojo/src/widget/html.js
3) http://localhost:8080/validations/stuts/dojo/src/widget.js
4) http://localhost:8080/validations/stuts/dojo/src/__package__.js
5) http://localhost:8080/validations/stuts/dojo/src.js
6) http://localhost:8080/validations/stuts/dojo/__package__.js

Also, on both FB and the page, I see the error:
Could not load 'dojo.widget.html.Tooltip'; last tried '__package__.js'
(I have enabled Dojo debug now). I think the Dojo widget is called
dojo.widget.Tooltip.

When the form first shows up I see the field error message for 'name' field.
When I fill out the field and tab out of it, no AJAX request is send to the
server (I am assuming that the expectation is that the form gets validated
at the server via an AJAX call onBlur).

When I click submit, an AJAX request is send and the server responds with
the HTML page for the action; but nothing happens to the page.

Here is the source of the page that is loaded into the browser when the form
is first shown:
<html>
<head>
<title>Testing Validations</title>
<link rel="stylesheet" href="/validations/struts/xhtml/styles.css"
type="text/css"/>
<script language="JavaScript" type="text/javascript">
    // Dojo configuration
    djConfig = {
        baseRelativePath: "/validations/struts/dojo",
        isDebug: true,
        bindEncoding: "UTF-8",
        debugAtAllCosts: true // not needed, but allows the Venkman debugger
to work with the includes
    };
</script>
<script language="JavaScript" type="text/javascript"
        src="/validations/struts/dojo/dojo.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/validations/struts/simple/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/validations/struts/ajax/dojoRequire.js"></script>
<script language="JavaScript" type="text/javascript"
        src="/validations/struts/CommonFunctions.js"></script>

</head>

<body>

<form namespace="" id="show" action="show.action" 
>
<table class="wwFormTable">
	<tr errorFor="show_name">
    <td align="center" valign="top" colspan="2"><span
class="errorMessage">Name cannot be empty</span></td>
</tr>
<tr>
    <td class="tdLabel"><label for="show_name"
class="errorLabel">Name:</label></td>
    <td
><input type="text" name="name" value="" id="show_name"/>

</td>
</tr>

	<tr>
    <td class="tdLabel"><label for="show_age"
class="label">Age:</label></td>
    <td
><input type="text" name="age" value="0" id="show_age"/>
</td>
</tr>

	<tr>
    <td colspan="2"><div align="right">    <input type="submit"
dojoType="struts:Bind" event="onclick"     value="Submit"  
  	id="show_0"  />

</div></td>
</tr>


<!-- javascript that is needed for tooltips -->
<script language="JavaScript"
type="text/javascript">dojo.require("dojo.widget.html.Tooltip");dojo.require("dojo.fx.html");</script>

</table>
</form>


</body>
</html>

Thanks,
Binil


Musachy Barroso wrote:
> 
> Enable dojo debugging by setting debug="true" on the head tag, and see if
> there are any errors. Can you post the errors that you are seeing in FB?
> 

-- 
View this message in context: http://www.nabble.com/Getting-the-AJAX-Form-Validations-to-work-tf3390223.html#a9437856
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: Getting the AJAX Form Validations to work

Posted by Musachy Barroso <mu...@gmail.com>.
Enable dojo debugging by setting debug="true" on the head tag, and see if
there are any errors. Can you post the errors that you are seeing in FB?

regards
musachy

On 3/12/07, Binil Thomas <bi...@gmail.com> wrote:
>
>
> Hi all,
>
> I am trying out the Ajax Form validations in S2. I see the validations
> example working perfectly fine in Showcase demo, but I can't get it to
> work
> on my machine.
>
> Here is what I did:
>
> 1) Created a maven project using the provided archetype.
> 2) Edited the struts.xml file to add my action:
> <struts>
>         ....
>         <package name="twms" extends="struts-default">
>                 <action name="show" class="validations.TryValidationAction"
> method="show">
>                         <result>index.jsp</result>
>                         <result name="input">index.jsp</result>
>                 </action>
>         </package>
> </struts>
> 3) Created src/main/java/validations/TryValidationAction.java as:
> package validations;
> import com.opensymphony.xwork2.ActionSupport;
> public class TryValidationAction extends ActionSupport {
>     public String show() {
>         System.out.println("Show: Name = " + name + ", age = " + age);
>         return SUCCESS;
>     }
>
>     private String name;
>     private int age;
>     // .. getters and setters for the two properties
> }
> 4) Created a
> src/main/resources/validations/TryValidationAction-show-validations.xmlfile
> as:
> <validators>
>         <field name="name">
>                 <field-validator type="requiredstring">
>                         <param name="trim">true</param>
>                         <message>Name cannot be empty</message>
>                 </field-validator>
>         </field>
> </validators>
> 5) Created src/main/webapp/twms/index.jsp as:
> <%@ taglib prefix="s" uri="/struts-tags" %>
> <html>
> <head>
> <title>Testing Validations</title>
> <s:head theme="xhtml"/>
> </head>
> <body>
> <s:form action="show.action" theme="xhtml">
>         <s:textfield label="Name" name="name"/>
>         <s:textfield label="Age" name="age"/>
>         <s:submit value="Submit"/>
> </s:form>
> </body>
> </html>
> 6) Configured decorators.xml to exclude my action.
>
> With this set up in place, I was able to load the form, and see the
> validations working when it is submitted. But when I change the theme to
> Ajax (in the head and the form), I get plenty of Dojo errors in FireBug, I
> don't get any validation errors and I am not able to submit the form.
>
> I am using version 2.0.3 of struts, as was set up by the archetype. Can
> someone help me get this working?
>
> Thanks,
> Binil
> PS: I can send my simple project setup, if anyone is interested.
> --
> View this message in context:
> http://www.nabble.com/Getting-the-AJAX-Form-Validations-to-work-tf3390223.html#a9436888
> Sent from the Struts - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
"Hey you! Would you help me to carry the stone?" Pink Floyd