Announcement

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

    Composite FormItem validation.

    SGWT: 4.1p
    FF: 26

    I have this custom CanvasItem that I'd like to have validate when form.validate() is called. I tried overloading validate (see below), but it doesn't seem to be called. Any suggestions?

    Thanks.
    Code:
    private static class DelayTimeItem extends CanvasItem{
    	private IntegerItem daysField;
    	private TimeItem timeField;
    	private DynamicForm form;
    	public DelayTimeItem(String name, String title){
    		super(name, title);
    		final ChangedHandler handler = new ChangedHandler(){
    			@Override
    			public void onChanged(ChangedEvent event) {
    				if(daysField.getValue() instanceof Integer && timeField.getValue() instanceof Date){
    					storeValue(convertValue());
    				}
    			}
    			private long convertValue() {
    				long minutes = 0;
    				Integer days =  (Integer) daysField.getValue();
    				Date time = (Date) timeField.getValue();
    				if(days != null) {
    					minutes += days * 24 * 60;
    				}
    				if(time != null) {
    					minutes = time.getHours() * 60 + time.getMinutes();
    				}
    				return minutes;
    			}
    		};
    		daysField = new IntegerItem("days"){{
    			setWidth(48);
    			setShowTitle(false);
    			addChangedHandler(handler);
    		}};
    		timeField = new TimeItem("time"){{
    			setWidth(64);
    			setShowTitle(false);
    			addChangedHandler(handler);
    		}};
    		form = new DynamicForm(){{
    			setFields(daysField, new StaticTextItem(){{
    				setShowTitle(false);
    				setContents("days");
    			}}, timeField);
    			setNumCols(3);
    		}};
    		setCanvas(form);
    		addShowValueHandler(new ShowValueHandler() {
    			@Override
    			public void onShowValue(ShowValueEvent event) {
    				Integer value = (Integer) event.getDataValue();
    				if(value != null){
    					long days = value / (24 * 60);
    					long minutes = value % (24 * 60);
    					daysField.setValue(days);
    					timeField.setValue(minutes / 60 + ":" + minutes % 60);
    				}
    			}
    		});
    	}
    	@Override
    	public Boolean validate() {
    		return super.validate() && daysField.validate() && timeField.validate();
    	}
    }

    #2
    Just declare a Custom validator. The logic within that validator can do anything, including directly inspecting the values of multiple contained fields if that's necessary for some reason.

    Comment


      #3
      Thanks.
      ======================

      Comment


        #4
        I have just noticed that when I call validate() on the form that contains my CanvasItem, my ShowValueHandler is getting called which was unexpected to me.

        Is that suppose to happen?

        SmartClient Version: v9.1p_2014-08-31/Pro Deployment (built 2014-08-31)

        Comment


          #5
          If the validator changes the value (as some can - see eg the transformTo property of pattern validators), then the new value has to be shown.

          Are you seeing showValue invoked without any change to the value?

          Comment


            #6
            I do not have any validators associated with that item and the value has not changed. I stepped into the code of my ShowValueHandler when the validate had called it and it contains the same value.

            Comment


              #7
              If, in addition to the code that triggered validation, you also did something like dynamicForm.setValues()/setValue() or FormItem.setValue(), you would expect the ShowValueHandler to be called.

              Likewise if the value is the same but is a different instance (can happen with date/time/datetime values).

              Or you might be checking the value via toString() or some other mechanism that would miss subtleties like going from a String to an Array with one String.

              If none of the above is the cause, and you're definitely seeing an unnecessary call that's creating a problem for you, if you can wrap it up into a standalone test case to eliminate any other causes we can take a look.

              Comment

              Working...
              X