Is there an easy way to perform an action, where validation of field A would trigger validation on field B (the allowed value in field B depends on value of field A)? I have implemented this using custom code (validators cross-referencing each other), but the solution isn’t perfect.
Here’s a description of the problem:
User sets a value in field B
User sets an incorrect value in field A
Both fields are immediately marked with error sign (setImmediate is set to true), since their combination is invalid. Both fields A and B have legal values per se, but it is the combination of these two that is invalid.
User corrects value in field A, and the field A error marker is removed immediately
However, field B error marker isn’t removed immediately. Instead, the error marker stays until next UIDL is transferred. In my case, Refresher component takes care of this, but with delay.
So, is there any way to trigger field B validation so that the error marker shows/disappears immediately after changing the value of field A? Is there any way to make field A validation result UIDL trigger validation on field B in client side (my solution triggers it server-side)? Or should the immediate validation of these two fields be implemented fully in client-side using JavaScript (of course they will be validated on the server-side too).
Both fields contain integers, and the fields are TextFields. Changing field type to some other (ComboBox etc.) isn’t possible, because our use case is specified this way.
I have two date fields: start date and end date. The start date must be smaller than the end date. Both fields are invalid, when the start date is greater than the end date. How can I do that with the Vaadin validation stuff.
I tried (Dummy code):
final DateField startField = DateField();
startField.setRequired(required);
startField.setValidationVisible(true);
startField.setImmediate(true);
startField.addValidator(...); // Validator that checks if start date is smaller than end date
final DateField endField = DateField();
endField .setRequired(required);
endField .setValidationVisible(true);
endField .setImmediate(true);
endField .addValidator(...); // Validator that checks if start date is smaller than end date
final ValueChangeListener changeValueListener = new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
startField.validate();
endField.validate();
}
};
startField.addListener(changeValueListener);
endField .addListener(changeValueListener);
OK, I think I’ve found a solution that works for me. After a call the validate()-method of my fields, I delete the component error. (See example)
final DateField startField = DateField();
startField.setRequired(required);
startField.setValidationVisible(true);
startField.setImmediate(true);
startField.addValidator(...); // Validator that checks if start date is smaller than end date
final DateField endField = DateField();
endField.setRequired(required);
endField.setValidationVisible(true);
endField.setImmediate(true);
endField.addValidator(...); // Validator that checks if start date is smaller than end date
final ValueChangeListener changeValueListener = new ValueChangeListener() {
@Override
public void valueChange(ValueChangeEvent event) {
try {
startField.validate();
startField.setComponentError(null); // MAGIC CODE HERE!!!
} catch (Exception e) {
// error found
}
try {
endField.validate();
endField.setComponentError(null); // MAGIC CODE HERE!!!
} catch (Exception e) {
// error found
}
}
};
startField.addListener(changeValueListener);
endField .addListener(changeValueListener);