[ 
https://issues.apache.org/jira/browse/WW-3954?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13541426#comment-13541426
 ] 

Lukasz Lenart commented on WW-3954:
-----------------------------------

Validation errors are returned as a JSON object, check to docs [1]

{code:javascript}
{
    "errors": ["Global Error 1", "Global Error 2"],
    "fieldErrors": {
        "field1": ["Field 1 Error 1", "Field 1 Error 2"],
        "field1": ["Field 2 Error 1", "Field 2 Error 2"]  
    }
}
{code}

so if your success: directive looks like this:

{code:javascript}
...
success: function(response) {
   if (response.errors || response.fieldErrors) {
       // handle errors -> showErrors()
   } else {
      // handle normal logic -> processResponse()
   }
}
{code}

When you active validateOnly, only validation will be performed, action result 
will not be returned - you can use that just to validate the form data - not to 
business logic.

To create a Maven project I use archetypes, you can do this as below:

{noformat}
mvn archetype:generate
{noformat}

or

{noformat}
mvn archetype:generate -DarchetypeCatalog=http://struts.apache.org/
{noformat}

and I'm using IntelliJ IDEA which natively opens Maven based project - directly 
from pom.xml and to launch them I'm using Maven Jetty plugin - you can see 
examples in the Struts2 apps [2]

[1] http://struts.apache.org/2.x/docs/ajax-validation.html
[2] https://svn.apache.org/repos/asf/struts/struts2/trunk/apps/blank/pom.xml

                
> Ajax Validation is broken (json)
> --------------------------------
>
>                 Key: WW-3954
>                 URL: https://issues.apache.org/jira/browse/WW-3954
>             Project: Struts 2
>          Issue Type: Bug
>          Components: Plugin - JSON
>    Affects Versions: 2.3.1, 2.3.1.1, 2.3.1.2, 2.3.3, 2.3.4, 2.3.4.1
>         Environment: Struts 2.3.4.1, Jquery 1.3.1, Struts json plugin 2.3.4.1
>            Reporter: COMBEAU
>            Assignee: Lukasz Lenart
>            Priority: Minor
>             Fix For: 2.3.9
>
>         Attachments: testAjaxValidation.zip
>
>
> Hello,
> I have find an issue on Struts 2.3.4.1. I have made a migration from Struts 
> 2.1.8 to Struts 2.3.4.1 and I have seen that the Ajax Validation doesn't work 
> anymore.
> Moreover, when I would like to make the validation of my form in Ajax I seen 
> that response was already "success" even if one or more fields are in errors. 
> I think it's not normal. In Struts 2.1.8 when fields are in errors the 
> response of an Ajax call is "error" not "success"
> To correct this issue I have made a workaround. In the 
> JSONValidationInterceptor, if the buildResponse method have "/*" in the first 
> append and "*/" in the last it correct the problem...
> why this :
> {code:java}
> protected String buildResponse(ValidationAware validationAware) {
>         //should we use FreeMarker here?
>         StringBuilder sb = new StringBuilder();
>         sb.append("/* { ");
>         ...
>         sb.append("} */");
>         /*response should be something like:
>          * {
>          *      "errors": ["this", "that"],
>          *      "fieldErrors": {
>          *            field1: "this",
>          *            field2: "that"
>          *      }
>          * }
>          */
>         return sb.toString();
>     }
> {code}
> as been replaced by this :
> {code:java}
> protected String buildResponse(ValidationAware validationAware) {
>         //should we use FreeMarker here?
>         StringBuilder sb = new StringBuilder();
>         sb.append("{ ");
>         ***
>         sb.append("}");
>         /*response should be something like:
>          * {
>          *      "errors": ["this", "that"],
>          *      "fieldErrors": {
>          *            field1: "this",
>          *            field2: "that"
>          *      }
>          * }
>          */
>         return sb.toString();
>     }
> {code}
> Is this a real issue ? 

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to