jquery - Send validate() addFieldError to ajax JSON result with Struts2 -


this question has answer here:

i send results of validate() method of action (json?) data ajax call on client side can inform user.

i sending form data in client side through ajax. in server side implementing simple validate() method (i no ready validation framework yet). ajax call

function save_new_user() {      var user =      {       username: $('#new_user_username').val(),             email: $('#new_user_email').val(),             password: $('#new_user_password').val()              };      data = {'user':user};      datastr = json.stringify(data);      $.ajax({         type : 'post',         url : 'savenewuser',         data: datastr,         datatype : 'json',         contenttype: 'application/json',         success : function(data, textstatus, jqxhr) {             if(data) {              }         },         error: function(jqxhr, textstatus, errorthrown) {             console.log(errorthrown);         }     }); } 

and in struts2 action have

public void validate() {     if(user.getusername().length() < 6){         addfielderror("user", "username must @ least 6 charachers long");     }      if(user.getpassword().length() < 8){         addfielderror("user", "password must @ least 8 charachers long");     } }  /* execute */ public string execute() throws exception  {      dbservices.saveuser(user);      return "success"; } 

i have default stack being applied action call

<package name="json-data" extends="json-default">     <interceptors>         <interceptor-stack name="jsondatastack">             <interceptor-ref name="json"/>             <interceptor-ref name="basicstack"/>             <interceptor-ref name="validation">                 <param name="excludemethods">input,back,cancel</param>             </interceptor-ref>             <interceptor-ref name="jsonvalidation"/>             <interceptor-ref name="workflow"/>         </interceptor-stack>      </interceptors>      <default-interceptor-ref name="jsondatastack"/>  </package> 

and validate() method being called.

so, here question once again: how can send results of validate() function data ajax call can use inform user?. seems method addfielderror() not configuring data in json readable form since error message in ajax result is

syntaxerror: unexpected token <(…) 

in this answer, regular expressions used filter fields error properties out of full action properties returned json object.

in case preferred add "fielderrors" property root object of result, instead of filtering regular expressions.

so, added action configuration (in case annotations)

@result(name="input", type="json", params={"root","fielderrors"}) 

and in ajax configuration, under success result, used returned json as

success : function(fielderrors, textstatus, jqxhr) {         (var property in fielderrors) {             if (fielderrors.hasownproperty(property)) {                 var this_field_err = fielderrors[property];                 $('#submit_result').append(property+" error");                 for(var ix=0; ix<this_field_err.length; ix++) {                     $('#submit_result').append(this_field_err[ix]);                     $('#submit_result').append("<br>");                  }             }         }     } 

this adds #submit_result div have page

username error: username must @ least 6 charachers long password error: password msut @ least 8 charachers long  

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -