Best way to validate ajax updates in JSF 2.0?

前端 未结 1 2015
隐瞒了意图╮
隐瞒了意图╮ 2021-02-04 18:39

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.

相关标签:
1条回答
  • 2021-02-04 18:51

    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 <f:validateXxx> tags. You can even create custom validators by implementing the Validator interface.

    E.g. checking the requireness:

    <h:inputText ... required="true">
        <f:ajax />
    </h:inputText>
    

    Or checking if it matches a pattern using one of the various <f:validateXxx> tags:

    <h:inputText ...>
        <f:validateRegex pattern="[a-z]+" />
        <f:ajax />
    </h:inputText>
    

    Or using a custom validator:

    <h:inputText ...>
        <f:validator validatorId="myValidator" />
        <f:ajax />
    </h:inputText>
    

    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 <f:ajax> 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 <f:ajax listener> method in order to submit the current input field by ajax. If you want to hook on a value change event, just use valueChangeListener.

    <h:inputText ... valueChangeListener="#{bean.valueChanged}">
        <f:ajax />
    </h:inputText>
    

    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.

    0 讨论(0)
提交回复
热议问题