struts-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "COMBEAU (JIRA)" <j...@apache.org>
Subject [jira] [Commented] (WW-3954) Ajax Validation is broken (json)
Date Sat, 29 Dec 2012 16:52:12 GMT

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

COMBEAU commented on WW-3954:
-----------------------------

I prepare my own stack based on jsonValidationWorkflowStack. When I tried to validate the
Form the return of my Ajax call is only success but it should be error....

Here is my stack :

 
<interceptor-stack name="jsonValidationWorkflowStackForTest">
  <interceptor-ref name="basicStack"/>
  <interceptor-ref name="validation">
      <param name="validateAnnotatedMethodOnly">true</param>
      <param name="excludeMethods">input,back,cancel</param>
  </interceptor-ref>
  <interceptor-ref name="jsonValidation"/>
  <interceptor-ref name="workflow"/>
</interceptor-stack>

Here is my method on my Java classes:

@Validations(requiredStrings = { @RequiredStringValidator(fieldName = "name", key = "required.firstName",
			shortCircuit = true), @RequiredStringValidator(fieldName = "pwd", key = "required.pwd",
			shortCircuit = true) })
	public String isValid() {
		em = "error message";
 
		return SUCCESS;
	}

And here is my jsp page that contains my Ajax call :

<script type="text/javascript">
function testValidation(){
	var testUrl = "<s:url action='isValid'/>";
	var myUrl = testUrl + "?struts.enableJSONValidation=true";
 
	var formId = '#formToBeValidated';
	var postData = $(formId).serialize();
 
	$.ajax({
	    type: "POST",
	    url: myUrl,
		dataType: 'json',
		cache : false,
		global: false,
		data: postData,
	    success:
	    	function(responseData) {
 
				clearErrors(formId);
 
				if (responseData.em != null) {
				.....
	            }
			},
	    error:
	    	 function(responseData) {
 
				clearErrors(formId);
 
	            //get errors from response
	            var text = responseData.responseText;
	            var errorsObject = getValidationErrors(text);
 
	            //show errors, if any
	            if(errorsObject.fieldErrors) {
	            	$.each(errorsObject.fieldErrors, function(i, error){
	            			$('#'+i).addClass('error');
	            			$('#'+i).before('<p class="required">'+error+'</p>');
	            	});
	            }
            }
 
	});
}
...
</script>
</head>
<body>
 
<s:form id="formToBeValidated" action="isValid" method="post" name="formToBeValidated">
	<s:textfield id="name" name="name" required="true"></s:textfield>
	<s:textfield id="pwd" name="pwd" required="true"></s:textfield>
</s:form>
 
	<button id="validation" onclick="testValidation();">valider</button>
</body>
                
> 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
>
>
> 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

Mime
View raw message