Validation - figuring out which FieldModel is invalid

10 views
Skip to first unread message

RAlfoeldi

unread,
Nov 25, 2010, 8:30:48 AM11/25/10
to GWT Pectin
Hi Andrew,

is there any (Pectin) way of figuring out exactly which FieldModel
isn't passing validation?

The use case is focusing on the widget that is causing problems,
possibly switching panels, views, whatever. (Think wizard with a
required field on the last dialog. Try to save and that dialog gets
shown.)

I could add a Validationhandler to every fieldValidator

_streetValid = new ValueHolder<Boolean>(Boolean.TRUE);

validateField(_street).using(
new NotEmptyValidator(CONSTANTS
.adresse_street_required())).when(_streetRequired);

ValidationPlugin.getFieldValidator(_street).addValidationHandler(
new ValidationHandler() {
public void onValidate(ValidationEvent event) {
_strasseValid.setValue(!
event.getValidationResult()
.contains(Severity.ERROR));
}
});

and check the ValueHolders...

... but that just doesn't seem right... (and is a lot of typing).

Is there any other Pectin way of achieving the same effect?

Greetings from Bern


Rainer

Andrew

unread,
Nov 25, 2010, 5:49:10 PM11/25/10
to GWT Pectin
Howdy,

There's nothing at this point that's elegant, but if you don't need a
value model then you can add something like the following to your form
model. It's a bit ugly but should hopefully work (I haven't tested
it).

public class MyFormModel extends FormModel {

public ArrayList<Field<?>> getInvalidFields() {
ArrayList<Field<?>> fields = new ArrayList<Field<?>>();
for (Field<?> field : allFields()) {
if (hasErrors(field)) {
fields.add(field);
}
}
return fields;
}

private boolean hasErrors(Field<?> field) {
ValidationManager validationManager =
getValidationManager(this);
if (field instanceof FieldModel) {
return validationManager.getValidator((FieldModel<?>)
field).getValidationResult().contains(Severity.ERROR);
} else if (field instanceof ListFieldModel) {
return validationManager.getValidator((ListFieldModel<?>)
field).getValidationResult().contains(Severity.ERROR);
}
// should probably barf here as we found a field type we don't
know about...
throw new IllegalStateException("Unknown field type: " +
field.getClass().getName());
}

Cheers
Andrew

RAlfoeldi

unread,
Nov 26, 2010, 3:06:35 AM11/26/10
to GWT Pectin
Hi Andrew,

thanks for the answer. That might not be "elegant" but its better than
what I built :-)

Greeting from Bern

Rainer
Reply all
Reply to author
Forward
0 new messages