0
votes

I am having trouble getting the Omnifaces skipValidators tag to work with Primefaces ajax events. Basically, I need to update many elements in a form based on the selection of a selectOneMenu, but I also need to retain any values already entered and not validate the form. However, I can't get it to work. Here's a small demonstration of the problem:

The xhtml:

            <h:form>
                <h:panelGrid columns="3">
                    <p:outputLabel for="console" value="Basic:" />
                    <p:selectOneMenu id="console" value="#{testBean.value}" style="width:125px">
                        <f:selectItem itemLabel="Select One" itemValue="" />
                        <f:selectItem itemLabel="Xbox One" itemValue="Xbox One" />
                        <f:selectItem itemLabel="PS4" itemValue="PS4" />
                        <f:selectItem itemLabel="Wii U" itemValue="Wii U" />
                        <p:ajax event="change" process="@form" update="@form">
                            <o:skipValidators/>
                        </p:ajax>
                    </p:selectOneMenu>
                    <p:message for="console"/>
                    <p:outputLabel for="reqField" value="RequiredField:"/>
                    <p:inputText id="reqField" required="#{testBean.required}" requiredMessage="REQUIRED!"/>
                    <p:message for="reqField"/>
                </h:panelGrid>
            </h:form>

The testBean:

@Named(value = "testBean")
@SessionScoped
public class TestBean implements Serializable {

    private String value;

    /**
     * Creates a new instance of TestBean
     */
    public TestBean() {
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public boolean isRequired() {
        return this.value != null && !this.value.isEmpty();
    }

}

When the page loads, it looks correct - there is no asterisk by reqField. Upon selecting one of the console values, the page changes correctly, and an asterisk appears next to the reqField. However, after a console values is selected, if 'Select One' is chosen again from the menu, a validation error is shown.

If I understand correctly, o:skipValidators should prevent the validation from happening. What's going on here?

I'm using omnifaces 2.3 and primefaces 5.1.12

2

2 Answers

0
votes

As mentioned in the showcase, the tag must be placed inside an UICommand or ClientBehaviorHolder component. You placed it inside a <p:ajax> which is neither. You should move it outside so it becomes direct child of <p:selectOneMenu> which is a ClientBehaviorHolder.

<p:selectOneMenu ...>
    ...
    <p:ajax ... />
    <o:skipValidators />
</p:selectOneMenu>
0
votes

I did without Ajax, TRY THIS ONE. Hope this helps!

The <o:validateBean> allows the developer to control bean validation on a per-UICommand or UIInput component basis, as well as validating a given bean at the class level.

The standard only allows validation control on a per-form or a per-request basis (by using multiple tags and conditional EL expressions in its attributes) which may end up in boilerplate code. The standard also, despite its name, does not actually have any facilities to validate a bean at all.

<o:validateBean disabled="true"/> that command will skip Validations

<p:commandButton 
            id="refresh"
            icon="fa fa-refresh"
            styleClass="refresh-button btn-blue"
            process="@form"
            update="phone_1 @form:htmlView">
   <o:validateBean disabled="true"/>
</p:commandButton>

OR

The <o:skipValidators> taghandler allows the developer to entirely skip validation when executing an UICommand or ClientBehaviorHolder action. This taghandler must be placed inside an UICommand or ClientBehaviorHolder component (client behavior holder components are those components supporting <f:ajax>).

<p:commandButton 
      id="refresh"
      icon="fa fa-refresh"
      styleClass="refresh-button btn-blue"
      process="@form"
      update="phone_1 @form:htmlView">
   <o:skipValidators/>
</p:commandButton>

If you want to know more just look here :

http://showcase.omnifaces.org/validators/validateBean http://showcase.omnifaces.org/taghandlers/skipValidators