Announcement

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

    Bug(?): SimpleType 'zipCodeUS' defined twice

    If I use a slightly modified showcase example (Forms.DataType Reuse) and show two textboxes instead of one with the zip-Code, the above warning is displayed.

    This happens not only if you do 'zipCodeField.setType(new ZipCodeUSType())' twice, but also if you use a variable and use that variable twice. Ex. 'zipCodeField.setType(myZipCodeUSType)'.

    The only way IMHO to avoid this kind of warning, is to use instantiate new SimpleType-Subclasses with different names, which is somehow against the reuse pattern.


    Are there better ways on defining new datatypes on datasources, or is this just a bug?

    Code:
    	public void onModuleLoad() {
    
    		DataSource dataSource = new DataSource();
    		ZipCodeUSType zipCodeUSType = new ZipCodeUSType();
    
    		DataSourceField zipCodeField = new DataSourceField();
    		zipCodeField.setName("zipCode");
    		zipCodeField.setTitle("Zip Code");
    		//zipCodeField.setType(new ZipCodeCustomType("nameType1"));
    		zipCodeField.setType(zipCodeUSType);
    
    		DataSourceField zipCodeField2 = new DataSourceField();
    		zipCodeField2.setName("zipCode2");
    		zipCodeField2.setTitle("Zip Code2");
    		//zipCodeField.setType(new ZipCodeCustomType("nameType2"));
    		zipCodeField2.setType(zipCodeUSType);
    
    		dataSource.setFields(zipCodeField, zipCodeField2);
    
    		final DynamicForm boundForm = new DynamicForm();
    		boundForm.setWidth(300);
    		boundForm.setDataSource(dataSource);
    
    		IButton button = new IButton("Validate");
    		button.addClickHandler(new ClickHandler() {
    			public void onClick(ClickEvent event) {
    				boundForm.validate();
    			}
    		});
    
    		VLayout layout = new VLayout(10);
    		layout.setMembers(boundForm, button);
    
    		layout.draw();
    	}
    
    	public static class ZipCodeUSType extends SimpleType {
    		public ZipCodeUSType() {
    			super("zipCodeUS", FieldType.TEXT);
    
    			RegExpValidator validator = new RegExpValidator("^\\d{5}(-\\d{4})?$");
    			setValidators(validator);
    		}
    	}
    	public static class ZipCodeCustomType extends SimpleType {
    		public ZipCodeCustomType(String name) {
    			super(name, FieldType.TEXT);
    
    			RegExpValidator validator = new RegExpValidator("^\\d{5}(-\\d{4})?$");
    			setValidators(validator);
    		}
    	}

    #2
    Create the type once, and pass it to setType() each time.

    Comment


      #3
      Code:
      ...
      ZipCodeUSType zipCodeUSType = new ZipCodeUSType();
      ...
      zipCodeField.setType(zipCodeUSType);
      ...
      zipCodeField2.setType(zipCodeUSType);
      As it is exactly done in the previously posted example? This is what causes the problem, and where the warning comes form, even if it shouldn't...

      The problem lies somewhere when the simpletype-component is created.
      Please consider the following code-snippet:
      Code:
      DataSource dataSource = new DataSource();
      dataSource.getOrCreateJsObj();
      boolean created = dataSource.isCreated();
      // created == TRUE
      
      Tree myTree = new Tree();
      myTree.getOrCreateJsObj();
      created = myTree.isCreated();
      // created == TRUE
      
      ZipCodeUSType zipCodeUSType = new ZipCodeUSType();
      zipCodeUSType.getOrCreateJsObj();
      created = zipCodeUSType.isCreated();
      // created == FALSE
      
      SimpleType myType = new SimpleType("myName", FieldType.TEXT);
      myType.getOrCreateJsObj();
      created = myType.isCreated();
      // created == FALSE
      For me such a behavior is not reasonable and therefore I would consider it as a bug, or am I wrong?

      Tested with SmartGWT nightly as of 2010-09-15, Gwt 2.0.4, Firefox 3.6.9

      Originally posted by Isomorphic
      Create the type once, and pass it to setType() each time.

      Comment


        #4
        Ah, sorry, we were looking at the commented out lines (which involved creating the type multiple times). Did you forget to call simpleType.register()?

        Comment


          #5
          Nope, a call to register() doesn't solve the problem.

          Following code DOESN'T WORK:
          Code:
          		DataSource dataSource = new DataSource();
          
          		ZipCodeUSType zipCodeUSType = new ZipCodeUSType();
          		zipCodeUSType.register();
          
          		DataSourceField zipCodeField = new DataSourceField();
          		zipCodeField.setName("zipCode");
          		zipCodeField.setTitle("Zip Code");
          		zipCodeField.setType(zipCodeUSType);
          
          		DataSourceField zipCodeField2 = new DataSourceField();
          		zipCodeField2.setName("zipCode2");
          		zipCodeField2.setTitle("Zip Code2");
          		zipCodeField2.setType(zipCodeUSType);
          
          		dataSource.setFields(zipCodeField, zipCodeField2);
          
          		final DynamicForm boundForm = new DynamicForm();
          		boundForm.setWidth(300);
          		boundForm.setDataSource(dataSource);
          
          		IButton button = new IButton("Validate");
          		button.addClickHandler(new ClickHandler() {
          			public void onClick(ClickEvent event) {
          				boundForm.validate();
          			}
          		});
          
          		VLayout layout = new VLayout(10);
          		layout.setMembers(boundForm, button);
          		layout.draw();
          But following code WORKS:
          Code:
          		DataSource dataSource = new DataSource();
          
          		ZipCodeUSType zipCodeUSType = new ZipCodeUSType();
          		zipCodeUSType.register();
          
          		DataSourceField zipCodeField = new DataSourceField();
          		zipCodeField.setName("zipCode");
          		zipCodeField.setTitle("Zip Code");
          		zipCodeField.setAttribute("type", "zipCodeUSType");
          
          		DataSourceField zipCodeField2 = new DataSourceField();
          		zipCodeField2.setName("zipCode2");
          		zipCodeField2.setTitle("Zip Code2");
          		zipCodeField2.setAttribute("type", "zipCodeUSType");
          
          		dataSource.setFields(zipCodeField, zipCodeField2);
          
          		final DynamicForm boundForm = new DynamicForm();
          		boundForm.setWidth(300);
          		boundForm.setDataSource(dataSource);
          
          		IButton button = new IButton("Validate");
          		button.addClickHandler(new ClickHandler() {
          			public void onClick(ClickEvent event) {
          				boundForm.validate();
          			}
          		});
          
          		VLayout layout = new VLayout(10);
          		layout.setMembers(boundForm, button);
          		layout.draw();
          As I mentioned before, IMHO there seems to be a problem when a SimpleType-object is created. DataSourceField.setType(SimpleType) triggers this wrong behavior, but the real problem may be somewhere else.
          Just consider the previous post, and you will see that after calling simpletype.getOrCreateJsObj (or .register which is basically the same), simpleType.isCreated returns false. This isn't the case on DataSource-objects and on tree-objects (as shown in the example), and probably on all others except simpletype too.

          Thanks in advance

          Comment


            #6
            This has been fixed. Please pick up the next nightly build.

            Thanks,
            Sanjiv

            Comment

            Working...
            X