Announcement

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

    suggestion: combobox value validation against picklist

    I'd like to know what's the best way to validate that user's entry in a comboBoxItem is part of the pickList associated with it.

    Quick context description:
    - The mainDS has a foreingKey on anotherDs. anotherDS is linked to anotherTable in my db.

    - the comboBoxItem is populated with the mainDS.foreignKey and the pickList is populated with values from anotherDS.

    - when the value is modified from the drop down, it's fine.

    - My problem is when the users type a value in the comboBoxItem. How should I proceed with minimum round trip between client-server to validate the value entered by the user matches a value from the pickList ?

    I'm currently using the ChangedHandler, catching every changedEvent (every time a user types a letter) and not updating the DS if the value is not in anotherTable.

    thanks

    #2
    There's a built-in validator type (hasRelatedRecord) - see this sample.

    Comment


      #3
      In my case the picklist data is very dynamic.
      So the following (more or less generic) custom validator works for me better.

      Code:
      public class IsOneOfDataArrivedValidator extends CustomValidator implements
      		DataArrivedHandler {
      
      	private RecordList dataArrivedList = null;
      
      	@Override
      	protected boolean condition(Object value) {
      		boolean check = false;
      		if (value != null) {
      			if (dataArrivedList != null && dataArrivedList.getLength() > 0) {
      				String fieldName = getFormItem().getName();
      				for (int i = 0; i < dataArrivedList.getLength() && !check; i++) {
      					Record r = dataArrivedList.get(i);
      					String recordValue = r.getAttribute(getFormItem()
      							.getValueFieldName());
      					// attention data type conversion
      					if (value instanceof Integer) {
      						check = value.equals(Integer.valueOf(recordValue));
      					} else {
      						check = value.equals(recordValue);
      					}
      				}
      			} else {
      				check = false;
      			}
      		} else {
      			check = true;
      		}
      		return check;
      	}
      
      	@Override
      	public void onDataArrived(DataArrivedEvent event) {
      		dataArrivedList = event.getData();
      	}
      
      }
      Usage:
      Code:
      IsOneOfDataArrivedValidator validator = new IsOneOfDataArrivedValidator();
      validator.setErrorMessage("error message");
      itemLevel.addDataArrivedHandler(validator);
      itemLevel.setValidators(validator);
      Regards
      Chris

      Comment

      Working...
      X