Announcement

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

    How to use setFieldError in ListGrid

    Hi,
    I tried to use the method setFieldError in a ListGrid, but it seems not to work like I expected.

    I wrote a small test:
    Code:
    public class TestGrid extends ListGrid {
    	public TestGrid() {
    		setWidth(500);
    		setHeight(300);
    		setLeft(200);
    		setTop(200);
    		
    		ListGridField field1 = new ListGridField("field1", "Field 1");
    		field1.setType(ListGridFieldType.TEXT);
    		field1.setCanEdit(true);
    		
    		ListGridField field2 = new ListGridField("field2", "Field 2");
    		field2.setType(ListGridFieldType.TEXT);
    		field2.setCanEdit(true);
    		
    		setFields(field1, field2);
    		setEditByCell(false);
    		setEditEvent(ListGridEditEvent.DOUBLECLICK);
    		
    		ListGridRecord[] r = new ListGridRecord[2];
    		r[0] = new ListGridRecord();
    		r[0].setAttribute("field1", "a");
    		r[0].setAttribute("field2", "b");
    		
    		r[1] = new ListGridRecord();
    		r[1].setAttribute("field1", "c");
    		r[1].setAttribute("field2", "d");
    		setData(r);
    		
    		addEditorExitHandler(new EditorExitHandler() {
    			public void onEditorExit(EditorExitEvent event) {
    				setFieldError(event.getRowNum(), "field1", "my error");
    				validateCell(event.getRowNum(), "field1");
    			}
    		});
    	}
    }
    I thought an error would appear after editing a cell - but it doesn't. What's my mistake?

    I know, I could write a CustomValidator. But as far as I don't want to do validation on a single cell, I'd like to use the setError-API.

    Thanks & best regards
    Thilo

    #2
    validateCell() clears existing errors before running validation so that errors that apply to previous values don't stick around when new values are entered. Therefore move your setFieldError() call after validateCell().

    Comment


      #3
      Hi,
      thank you very much for your reply!

      I tested a bit more and removed the call of validateCell().

      The problem seems to be the EditorExitEventHandler. This seems to be the wrong event.

      I changed my handler to this call:
      setFieldError(1, "field1", "my error");

      When I edit row 2, the error is shown on row 1. If I edit row 1, no error is shown.

      So, which is the correct event for such a validation?

      Thanks & best regards
      Thilo

      Comment


        #4
        Declare a DataSource with a primaryKey (required for editing).

        Comment


          #5
          Hi,
          I'm still dealing with that issue. Now it works for my ListGrids, but not for TreeGrids.

          The TreeGrid is configured like this (the DataSource has a PrimaryKey and is set to client only):
          Code:
          treeGrid.setDataSource(treeGridDS);
          treeGrid.setLoadDataOnDemand(false);
          treeGrid.setSaveLocally(true);
          treeGrid.setAutoFetchData(true);
          treeGrid.setAutoSaveEdits(true);
          treeGrid.setCanEdit(true);
          For adding nodes, I use the Tree-API, like this:
          Code:
          Tree tree = treeGrid.getTree();
          TreeNode n = new TreeNode();
          n.setAttribute....
          tree.add(n, tree.getRoot());
          or
          Code:
          tree.add(n, treeGrid.getSelectedRecord());
          On the editCompleteEvent, I do some validation on the records and mark some of them with errors:
          Code:
          treeGrid.setFieldError(row, "uniqueField", "My Message");
          Now, this works fine for all rows, but not the one which was currently edited!

          I tried not to use the Tree-API, but treeGrid.addData(n) directly. In hosted mode, this works fine (I can add and edit nodes as well as set errors!), but in the IE, I get a Stackoverflow on adding.

          So, how can I achieve Field Errors?

          BTW: Id would be nice to have an API to set FieldErros on Trees. Working with rows in TreeGrids is a bit confusing.

          Thanks & best regards
          Thilo

          Comment


            #6
            Not sure why you can't get this to work with TreeGrid, it's exactly the same as ListGrid. Again check the timing of when built-in validation is performed vs your additional errors. Probably you have a different setting for editByCell, saveByCell, or a similar setting.

            Comment


              #7
              Hi,
              the last weeks I was engaged in some other stuff. Now I came back to this issue, which is still open to me. I wrote a new test:
              Code:
              public TestTree(AppController controller) {
              		DataSourceTextField idField = new DataSourceTextField("id");
              		idField.setPrimaryKey(true);
              		DataSourceIntegerField parentField = new DataSourceIntegerField("parent");
              		parentField.setForeignKey(id + ".id");
              		DataSourceTextField nameField = new DataSourceTextField("name");
              		DataSource ds = new DataSource();
              		ds.setFields(idField, parentField,  nameField);
              		ds.setClientOnly(true);
              		
              		final TreeGrid treeGrid = new TreeGrid();
              		treeGrid.setSaveLocally(true);
              		treeGrid.setLoadDataOnDemand(false);
              		treeGrid.setWidth(200);
              		treeGrid.setHeight(100);
              		treeGrid.setDataSource(ds);
              		treeGrid.setEditEvent(ListGridEditEvent.DOUBLECLICK);
              		treeGrid.setCanEdit(true);
              		treeGrid.setValidateOnChange(false);
              		treeGrid.setValidateByCell(false);
              		
              		ListGridField field = new ListGridField("name");
              		field.setCanEdit(true);
              		treeGrid.setFields(field);
              		treeGrid.fetchData();
              		
              		treeGrid.addEditCompleteHandler(new EditCompleteHandler() {
              			public void onEditComplete(EditCompleteEvent event) {
              				GWT.log("Set error on row: " + event.getRowNum(), null);
              				treeGrid.setFieldError(event.getRowNum(), "name", "My Error");
              			}
              		});
              		
              		IButton button = new IButton("Add TopLevel");
              		button.addClickHandler(new ClickHandler() {
              			public void onClick(ClickEvent event) {
              				Tree tree = treeGrid.getTree();
              				TreeNode root = tree.getRoot();
              				TreeNode node = new TreeNode();
              				node.setIsFolder(true);
              				node.setAttribute("id", "id"+i);
              				node.setAttribute("name", "Name");
              				node.setAttribute("parent", root.getAttribute("id"));
              				tree.add(node, root);
              				i++;
              			}
              		});
              		
              		IButton button2 = new IButton("AddChild");
              		button2.addClickHandler(new ClickHandler() {		
              			public void onClick(ClickEvent event) {
              				TreeNode parent = (TreeNode) treeGrid.getSelectedRecord();
              				if (treeGrid == null)
              					return;
              					
              				TreeNode node = new TreeNode();
              				node.setIsFolder(true);
              				node.setAttribute("id", "id"+i);
              				node.setAttribute("name", "Name");
              				node.setAttribute("parent", parent.getAttribute("id"));
              				
              				Tree tree = treeGrid.getTree();
              				tree.add(node, parent);
              				tree.openAll();
              				i++;
              			}
              		});
              
              		this.addMember(treeGrid);
              		this.addMember(button);
              		this.addMember(button2);
              	}
              As you can see, I use a client-only datasource and a primary key. No other validation is done. New nodes can be added via the 2 buttons.

              I would expect errors to appear after editing a name in the treegrid. The logger tells me, that the according code is executed, but I don't see an error.

              Have you any more hints for me?

              BTW: treeGrid.setFieldError(event.getRowNum()+1, "name", "My Error"); works fine if this row exists.

              Thanks & kind regards
              Thilo

              Comment


                #8
                That seems like it should work. Just to eliminate it as a possible timing issue, can you try calling setFieldError() after a delay (use DelayCommand).

                Comment


                  #9
                  Hi,
                  it seems to be a timing issue. I'm not sure, what mean by DelayCommand. I adjusted my code to this:
                  Code:
                  treeGrid.addEditCompleteHandler(new EditCompleteHandler() {
                  	public void onEditComplete(EditCompleteEvent event) {
                  		GWT.log("Set error on row: " + event.getRowNum(), null);
                  		final int row = event.getRowNum();
                  		Timer t = new Timer() {
                  			public void run() {
                  				treeGrid.setFieldError(row, "name", "My Error");
                  			}
                  		};
                  		t.schedule(100);
                  	}
                  });
                  And the error appears!

                  So, should I use a timer or do you have a better workaround?

                  Thanks & best regards
                  Thilo

                  Comment

                  Working...
                  X