Fuv
Fuv

Reputation: 922

How to skip validation phase in p:ajax

I bumped into that problem today at work. To make long story short I want to skip validation phase (in fact also not check required attribute) while using p:ajax component, though I want to update model in bean.

To illustrate the problem:

View:

<h:form>
    <p:messages id="msg" />
    <p:selectBooleanCheckbox value="#{bean.flag}">
        <p:ajax listener="#{bean.flagChanged}" update="@form" />
    </p:selectBooleanCheckbox>
    <p:inputText value="#{bean.value}" required="true"
        disabled="#{bean.flag}" validator="#{bean.validate}">
        <p:ajax event="keyup" update="msg" />
    </p:inputText>
    <p:commandButton />
</h:form>

Bean:

public class Bean {
    private Integer value;
    private Integer prevValue;
    private boolean flag;

    public void flagChanged() {
        if (!flag) {
            value = prevValue;
        } else {
            prevValue = value;
            value = null;
        }
    }

    // value and flag setters and getters

    public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) {
        Integer number = (Integer) value;

        if (number == 7) {
            throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "7 is my lucky number", ""));
        }
    }
}

Checkbox is to disable and enable input text simultaneously hiding value when it is disabled and restoring it when it is enabled. Problem occures when user fills wrong value that is validated immediately and therefore not updated into the bean. So after clicking and unclicking checkbox previous correct value is restored. The very same situation happens while deleting previous value and leaving blank field (required is set to true and hence again previous value is restored).

I tried to set immediate=true, but that changes nothing, so I'm wondering if there is any way to skip validation phase in p:ajax. Maybe I am trying to achieve this in not exactly proper way (any hints appreciated!), but I would like to know if that approach is somehow possible to accomplish.

Upvotes: 2

Views: 3744

Answers (2)

Fuv
Fuv

Reputation: 922

I thought there is built-in solution that solves that problem. As no one seems to exist I present my workaround to this problem (although it's not really answer to my question, becouse validation is still processed, I just ignore validation in my own validator).

<h:form>
    <p:messages id="msg" />
    <p:selectBooleanCheckbox value="#{bean.flag}">
        <p:ajax listener="#{bean.flagChanged}" update="@form" />
    </p:selectBooleanCheckbox>
    <p:inputText value="#{bean.value}"
        required="#{not empty param[submitButton.clientId]}" disabled="#{bean.flag}"
        validator="#{bean.validate}">
        <p:ajax event="keyup" update="msg" />
    </p:inputText>
    <p:commandButton action="#{bean.submit}" update="@form" binding="#{submitButton}">
        <f:param name="validate" value="true" />
    </p:commandButton>
</h:form>

Validation method:

public void validate(FacesContext facesContext, UIComponent uiComponent, Object value) {
    Map<String, String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    if (!params.containsKey("validate")) {
        return;
    }
    Integer number = (Integer) value;

    if (number == 7) {
        throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_ERROR, "7 is my lucky number", ""));
    }
}

Upvotes: 3

Mr.J4mes
Mr.J4mes

Reputation: 9266

One way I can think of is to use <p:commandButton> with ajax="false" to submit the form. In addition, in your validator method, add the following line to check for ajax submission of the value:

if (FacesContext.getCurrentInstance().isPostback()) return;

In this case, using immediate="true" in <p:ajax> does not help because the <p:inputText> component will still be converted and validated as usual. The only difference is that it would go through this process earlier than other components on the page. Hope this helps :)

Upvotes: 1

Related Questions