We tried to implement Custom Validator on a LookupPickerField component.
For the validator part, it’s able to display error message accordingly.
However, we are not sure how to gain control of the UI component such that its able to display something like the default “red border” of TextField “required field”.
How should we make the UI component with Custom Validator react and behave consistently as with other default UI?
Mortoza, thank for the input.
The field that we are trying to validate is not based on the “mandatory” field itself. However, the validator is for checking other type of constraints. In this particular case: "When the managedBy field of the Department Entity is changed, we want to check whether the Employee selected as the manager is still assigned to the other department."
And we want to use the same “Red Border” around the field for consistent display language to point out the error LookupPickerField after the validator identified the error.
Hi Jeremy, Sorry, I miss-understood what you’re looking for. I think you will have to use CSS programmatically. But I let the CUBA experts guide us. This is interesting to me too.
You can trigger validation of a Field component using validate() method of the component or validate(fields) method of a Window. Standard fields will reset old state of validation hint, perform validation of a value using added Validators and then change border/background to red with an appropriate error hint.
Hello, Yuriy!
I have cuba-platform 5.4 and the following code in my controller and xml.
Validation occurs and notification show ok, but there is no red border and tip for the fields.
Fields without custom validator show both the red border and the tip.
Could you advice something?
public void init(Map<String, Object> params) {
super.init(params);
startTimeField.addValidator(new Field.Validator() {
@Override
public void validate(Object o) throws ValidationException {
Date startTime = (Date) startTimeField.getValue();
Date endTime = (Date) endTimeField.getValue();
if (startTime != null && endTime != null &&
(startTime.after(endTime) || startTime.equals(endTime))) {
throw new ValidationException("courtbookingslot.editor.time.validation.message");
}
}
});
startTimeField.addListener(new ValueListener() {
@Override
public void valueChanged(Object source, String property, @Nullable Object prevValue, @Nullable Object value) {
validate(Collections.singletonList((Validatable) startTimeField));
}
});
}