> To: user@struts.apache.org > Subject: RE: Textfield onchange --> handling ajax error response to show > action errors > From: christoph.nenn...@lex-com.net > Date: Wed, 2 Sep 2015 10:08:16 +0200 > > The documentation about ajax-validation might help you: > > http://struts.apache.org/docs/ajax-validation.html > > > Basically you have to create JSON with 'errors' like this: > > http://struts.apache.org/docs/ajax-validation.html#AJAXValidation-Howitworks > > > And you might use the JavaScript function > StrutsUtils.showValidationErrors( ) to display them. Of course you must > include struts utils.js to use that function.
MG>this would assume your errors are JSON formatted as: { "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"] }} MG> MG>in StrutsUtils.getValidationErrors below if data is POJO return the POJO object MG>otherwise return from position 2 after the /* as seen here StrutsUtils.getValidationErrors = function(data) { if (typeof data === "object") { return data; } else { if (data.indexOf("/* {") === 0) { // /* { must be in position 0 return eval("( " + data.substring(2, data.length - 2) + " )"); } else { return null; } } }; MG>later on in StrutsUtils.showValidationErrors johann looks for the enclosing Div as seen here: var enclosingDiv = findWWGrpNode(elem); // find wwgrp div/span MG>so if your jsp contains NO div...then this wont work MG>I always have 2 div tags in jsp: MG>one for normal output response with green color: <div id="ok" style="width:50px; height:50px; background-color:green">WORKS</div> MG>one for error output response with red color: <div id="error" style="width:50px; height:50px; background-color:red">DOESNT WORK</div> MG>in postValidation method up to receiving the text function postValidation(request) { var form = $('form'); //clear previous validation errors, if any StrutsUtils.clearValidationErrors(form); //get errors from response var text = request.responseText; //this is where I would test text for error response or normal response if(text.indexOf("error")!=-1) { if(isJson(text) == "false") error.innerHTML = text; //normal error text else error.innerHTML = JSON.parse(text); //JSON error text } else { if(isJson(text) == "false") ok.innerHTML = text; //normal response text else ok.innerHTML = JSON.parse(text); //JSON response text form.submit(); } } //end postValidation function isJson( jsonData ) if (typeof jsonData == 'string') { if (! /^[\[|\{](\s|.*|\w)*[\]|\}]$/.test(jsonData)) { return "true"; } } try { jsonData = $.parseJSON(jsonData); } catch (e) { return "false"; } //any exception will return false return "true"; //valid JSON formatted data ) //end isJson https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse http://stackoverflow.com/questions/3710204/how-to-check-if-a-string-is-a-valid-json-string-in-javascript-without-using-try http://www.w3schools.com/jsref/jsref_indexof.asp HTH Martin > > > > regards, > Christoph > > > > > > From: fea jabi <zy...@hotmail.com> > > To: "user@struts.apache.org" <user@struts.apache.org>, > > Date: 01.09.2015 14:37 > > Subject: RE: Textfield onchange --> handling ajax error response to > > show action errors > > > > Can you point me to a link that has the sample which does this > > please? I tried but couldn't get the result. > > > > Thanks, for trying to help me on this. > > > > > > > > > From: lukaszlen...@apache.org > > > Date: Sun, 30 Aug 2015 15:22:52 +0200 > > > Subject: Re: Textfield onchange --> handling ajax error response > > to show action errors > > > To: user@struts.apache.org > > > > > > <s:actionerror/> works on server side but JavaScript works on client > > > side - it isn't possible to join them. You can only mimic its > > > behaviour by adding the same html structure as <s:actionerror/> does. > > > > > > 2015-08-28 17:14 GMT+02:00 fea jabi <zy...@hotmail.com>: > > > > text-field onchange ---> making ajax call. > > > > > > > > > > > > onsucess ---> everything works fine as needed > > > > > > > > > > > > on failure ----- > like user entered wrong data ---> How to show > > the error messages in jsp. I added fieldError but not sure how to > > show it in jsp with the ajax stuff. > > > > > > > > In jsp > > > > <s:actionerror/> > > > > .................... > > > > > > > > <script> ........$.ajax({ > > > > > > > > url: "ajax/retrivexxxAJAXAction.action"+"? > > jsonRequestdata="+JSON.stringify(json), > > > > type: 'POST', > > > > dataType: 'json', > > > > > > > > > > > > success:function(response){ > > > > ....................... > > > > }error:function(jqXhr, textStatus, errorThrown){ > > > > > > > > alert(textStatus);} > > > > > > > > }); > > > > ........ > > > > > > > > > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: user-unsubscr...@struts.apache.org > > > For additional commands, e-mail: user-h...@struts.apache.org > > > > > > > This Email was scanned by Sophos Anti Virus