ROO, Dojo and Spring.ValidateAllDecoration: howto have an alert when validation fails
Hi all, I'm using Roo for an e-commerce application. I've used all Spring tools to perform input validation client and server side with Spring JS-Dojo and bean validation.
Everything works fine.
Now I need to add an alert in case of form validation failure, but it seems to be hard:
my form has following structure:
Code:
<form:form method="POST" modelAttribute="user">
<div>
<form:errors cssClass="errors" delimiter="<p/>" htmlEscape="false"/>
<form:input id="firstname" path="firstname" />
<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : 'firstname', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: 'Insert First Name', invalidMessage: 'invalid name', required: 'true'}})); </script>
<form:input id="lastname" path="lastname" />
<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : 'lastname', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: 'Insert LastName', invalidMessage: 'invalid surname', required: 'true'}})); </script>
...
<input id="proceed" type="submit" value="Create account" />
<script type="text/javascript">
Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'proceed', event:'onclick'}));
</script>
</div>
</form:form>
....
I've tried to integrate code snippet found on dojo website (http://dojotoolkit.org/reference-gui...rative-example), but aso this code seems to be ignored:
Code:
<form:form method="POST" modelAttribute="user" dojoType="dijit.form.Form" >
<div>
<form:errors cssClass="errors" delimiter="<p/>" htmlEscape="false"/>
<script type="dojo/method" event="onSubmit">
if (this.validate()) {
return confirm('Form is valid, press OK to submit');
} else {
alert('Form contains invalid data. Please correct first');
return false;
}
return true;
</script>
<form:input id="firstname" path="firstname" />
<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : 'firstname', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: 'Insert First Name', invalidMessage: 'invalid name', required: 'true'}})); </script>
<form:input id="lastname" path="lastname" />
<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : 'lastname', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: 'Insert LastName', invalidMessage: 'invalid surname', required: 'true'}})); </script>
...
<input id="proceed" type="submit" value="Create account" dojoType="dijit.form.Button" />
<script type="text/javascript">
Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'proceed', event:'onclick'}));
</script>
</div>
</form:form>
....
I've tried also adding this code snippet to see if my javascript were executed, but nothing: the alert doesn't appear:
Code:
<form:form method="POST" modelAttribute="user">
<div>
<form:errors cssClass="errors" delimiter="<p/>" htmlEscape="false"/>
<form:input id="firstname" path="firstname" />
<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : 'firstname', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: 'Insert First Name', invalidMessage: 'invalid name', required: 'true'}})); </script>
<form:input id="lastname" path="lastname" />
<script type="text/javascript">Spring.addDecoration(new Spring.ElementDecoration({elementId : 'lastname', widgetType : 'dijit.form.ValidationTextBox', widgetAttrs : {promptMessage: 'Insert LastName', invalidMessage: 'invalid surname', required: 'true'}})); </script>
...
<script type="text/javascript">
function validateForm() {
alert('Form validated (fake validation for test purposes)');
return true;
}
</script>
<input id="proceed" type="submit" value="Create account" onclick="return validateForm();"/>
<script type="text/javascript">
Spring.addDecoration(new Spring.ValidateAllDecoration({elementId:'proceed', event:'onclick'}));
</script>
</div>
</form:form>
....
I'm not an expert of Javascript nor Dojo toolkit, Spring JS documentation is very limited, and I cannot use Dojo directly for my purposes: any idea on what's wrong with my code?
Thanks in advance for your responses.
Daniele