Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    DynamicForm.submit with error callback

    Hello
    DynamicForm.submit takes a DSCallback which is invoked only if there are no errors in the returned JSON response (ie the 'status' property is 0). If any errors, the Callback doesn't seem to get invoked. Is it intended behavior? How do I get a callback from submit, irrespective of whether status is 0 or negative number?
    here's my sample code:
    Code:
    final DynamicForm form = new DynamicForm();
    	DataSourceTextField dfname = new DataSourceTextField("name");
    		DataSource ds = new DataSource();
    		ds.setFields(dfname);
    		ds.setDataURL("/app/test-submit");
    		ds.setDataProtocol(DSProtocol.POSTMESSAGE);
    		ds.setDataFormat(DSDataFormat.JSON);
    		form.setDataSource(ds);
    
                    TextItem name = new TextItem("name", "NAME");
    		form.setFields(name);
    
                    IButton btn = new IButton("SUBMIT");
    		btn.addClickHandler(new ClickHandler() {			
    			@Override
    			public void onClick(ClickEvent event) {
    				form.submit(new DSCallback() {
    					@Override
    					public void execute(DSResponse response, Object rawData, DSRequest request) {
    		                                // breakpoint on following line is hit only if response json has status == 0 
    						Map<String,String> errors = response.getErrors();
    					}
    				});
    			}
    		});
    The response from server looks like this:
    Code:
    {"response":{"endRow":0,"totalRows":1,"startRow":0, "status":-1, "error":{"f1":"FAKE ERROR"}, "data":[{"id": 100, "beginDate":"2010-03-21T09:12:33"}]}}
    Any ideas?

    #2
    You can register an error handler on the datasource.

    Comment


      #3
      Thanks. I added this:
      Code:
      ds.addHandleErrorHandler(new HandleErrorHandler() {
      			@Override
      			public void onHandleError(ErrorEvent event) {
      				DSResponse resp = event.getResponse();
      				Map<String,String> errors = resp.getErrors();
      				for(String k : errors.keySet()) {
      					SC.say(k + " => " + errors.get(k));
      				}
      			}
      		});
      The handler does get invoked but the 'errors' map seems to be empty - errors.keySet() is null!. Here's the response from the server:
      Code:
      {"response":{"endRow":0,"totalRows":1,"startRow":0, "status":-1, "errors":{"name":{"errorMessage":"FAKE ERROR"}}, "data":[{"id": 100, "beginDate":"2010-03-21T09:12:33"}]}}
      So, errors are being returned (above is from SmartGWT development console). Not sure why the DSResponse.getErrors doesn't see it.
      Also, the documentation for addHandleErrorHandler says to return false from the hook - however the return type is declared to be 'void' ! What am I missing?
      thanks again.

      Comment


        #4
        to answer my own question, the right way to do is to override DataSource's transformResponse and set status on the response:
        Code:
        @Override
        	protected void transformResponse(DSResponse response, DSRequest request, Object data) {
        		super.transformResponse(response, request, data);
        		
        		MYResponse r = (MYResponse) data;
                        if (!r.isSuccess()) {               
                          response.setStatus(RPCResponse.STATUS_VALIDATION_ERROR);  
                          response.setErrors(r.getErrors());
                        }
        	}
        where MYResponse is
        Code:
        public class MYResponse extends JavaScriptObject {
        	protected MYResponse() {}
        	public final native int      getStatus() /*-{ return this.response.status; }-*/;
        	public final native boolean  isSuccess() /*-{ return this.response.status == 0; }-*/;
        	public final native JavaScriptObject   getErrors() /*-{ return this.response.errors; }-*/;
        	public final native JavaScriptObject getData() /*-{ return this.response.data; }-*/;	
        }
        But the documentation for form.submit should make it clear that the callback is invoked only upon success. It currently says 'callback to invoke upon completion' - error case is also 'completion'.

        Comment

        Working...
        X