Our team is writing its first JSF 2.0 application since using Stripes for many years, and I have some questions on the best way to use the f:ajax tag and validate the input.
You're looking in the wrong direction to achieve the concrete functional requirement of validating an input field. You should use a normal JSF validator for this, not some ajax listener method which runs at the wrong moment (the INVOKE_ACTION
phase instead of PROCESS_VALIDATIONS
phase) and where you don't directly have a hand at the model value. The ajax listener method is merely to be used to execute some business logic based on the current model value(s).
JSF has several builtin validators behind the required
attribute and several
tags. You can even create custom validators by implementing the Validator interface.
E.g. checking the requireness:
Or checking if it matches a pattern using one of the various
Or using a custom validator:
with
@FacesValidator("myValidator")
public class MyValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) {
if (value is not valid) {
throw new ValidatorException(new FacesMessage(...));
}
}
}
The
is merely there to submit the current input field during the HTML DOM change
event (or click
event in case of checkboxes/radiobuttons). You don't necessarily need a
method in order to submit the current input field by ajax. If you want to hook on a value change event, just use valueChangeListener
.
with
public void valueChanged(ValueChangeEvent event) {
Object oldValue = event.getOldValue();
Object newValue = event.getValue();
UIComponent component = event.getComponent();
// ...
}
Note that this will only be invoked when validation has passed on the particular component.