How to create a custom validator in smartGWT?
Asked Answered
W

3

5

I got 2 TimeItems, I want to be able to validate that the value of the second item is not bigger than the first.

I'm aware that I must inherit from CustomValidator and place my validation logic in #condition, I can retrieve the value of the validated item with #getFormItem, but I got no idea on how to pass the value of the first field onto the validator

Warms answered 23/3, 2012 at 10:1 Comment(0)
C
7

Or for better readability and code maintainability, use a nested class:

class YourClass {
    private TimeItem timeItem1;
    private TimeItem timeItem2;

    public YourClass() {
       //Instantiate your TimeItem objects.
       ...

       //Set the validator
       MyCustomValidator validator = new MyCustomValidator();
       timeItem1.setValidators(validator);

       /Assuming that both items should check validation.
       timeItem2.setValidators(validator);
    }

    ...
    class MyCustomValidator extends CustomValidator  {

         @Override
         protected boolean condition(Object value) {
            //Validate the value of timeItem1 vs the timeItem2
            //Overide equals or transform values to 
            if (timeItem1.getValueAsString().equals(timeItem2.getValueAsString()) {

            //Return true or false.
            }
          return false;
         }
    ...
    }
}

And if you prefer create getter methods for the two TimeItem objects to avoid using private attributes in your nested class.

Celestaceleste answered 23/3, 2012 at 19:38 Comment(2)
thx, was hoping to have a separate and reusable validator, but yes I can access both fields if the validator is within the same class, that would doWarms
If you want a separate class, just define a constructor with the two objects as its arguments: public MyCustomValidator(TimeItem timeItem1, TimeItem timeItem2), and assign these two objects in two copy private attributes you can use in your condition method ...Celestaceleste
A
2

Can't you do something like:

CustomValidator cv = new CustomValidator() {

        @Override
        protected boolean condition(Object value) {
            if (otherTimeItem.getValue()<value){
                return true;
            }else
                return false;
            }
        }
};

Then set you TimeItem validator:

timeItem.setValidators(cv);

Of course you can't use '<' to compare the values of your TimeItems. But convert to time objects and compare them.

Augustaaugustan answered 23/3, 2012 at 10:54 Comment(0)
T
0

I know this is an old post, but I think there is a better solution (at least in the more recent versions of SmartGWT). So here it goes for anyone interested:

You can get the whole record being validated using getRecord(). And then, you can get any field value by just querying the record's attributes:

CustomValidator validator = new CustomValidator() {
    @Override
    protected boolean condition(Object rawValue) {
        Record validatedRecord = getRecord();
        String field1 = validatedRecord.getAttribute(FIELD1_NAME);
        String field2 = validatedRecord.getAttribute(FIELD2_NAME);

        return field2 <= field1;
    }
}

This is better because you don't need to keep references to your fields.

Titanate answered 9/9, 2016 at 23:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.