user2824073
user2824073

Reputation: 2485

Validation Constraint blocking actionListener

I'm coding a JSF application which includes in the upper part of it a form with fields and in the lower part of it a datatable with a delete button on each item:

            <h:panelGrid columns="2" styleClass="default">

                <h:outputLabel for="name" value="Name: " />



                <h:inputText id="name" value="#{user.name}" />

                <h:outputLabel for="name" value="Surname: " />
                <h:inputText id="surname" value="#{user.surname}" />

                <h:outputLabel for="name" value="Email: " />
                <h:inputText id="email" value="#{user.email}" />



                <h:commandButton actionListener="#{manager.save}"
                    styleClass="buttons" value="Save" />
                <h:messages errorStyle="color: red" infoStyle="color: green"  />
                <br/>
                <ui:debug/>
            </h:panelGrid>

           <br/>
            <h:dataTable value="#{userList}" var="item" styleClass="table"
                headerClass="table-header"
                rowClasses="table-odd-row,table-even-row">
                <h:column>
                    <f:facet name="header">Name</f:facet>
                    <h:outputText value="#{item.name}" />
                </h:column>
                <h:column>
                    <f:facet name="header">Surname</f:facet>
                    <h:outputText value="#{item.surname}" />
                </h:column>

                <h:column>
                    <f:facet name="header">Email</f:facet>
                    <h:outputText value="#{item.email}" />
                </h:column>
                <h:column>
                    <f:facet name="header">Delete</f:facet>
                    <h:commandButton actionListener="#{manager.delete(item)}"
                        styleClass="buttons" value="Delete" />
                </h:column>

            </h:dataTable>

        </h:form>

The application worked until I have applied constraints on the User Bean:

public class User  {

@Size(min = 5, max = 20, message = "Please enter a valid name (5-20 characters)")
private String name;
@Size(min = 5, max = 20, message = "Please enter a valid surname (5-20 characters)")
private String surname;
@Size(min = 5, message = "Mail size is at least 5 characters")
@Pattern(regexp = "[a-zA-Z0-9]+@[a-zA-Z0-9]+\\.[a-zA-Z0-9]+", message = "Email format is invalid.")
private String email;
}

Now when I hit the Delete button on my page - with the text fields blank- the Constraint validator kicks in, preventing the actionListener to execute. Is there a way to work around this issue ? Thanks a lot !

Upvotes: 0

Views: 107

Answers (1)

perissf
perissf

Reputation: 16273

Since you are not using ajax, the whole form is submitted and the validators are automatically fired.

In order to prevent this, you can either use ajax, in order to submit only the portion of form that you want to submit, or use two different forms.

With ajax you can do something like:

<h:dataTable id="dataTable" ...>
    ...
    <h:commandButton actionListener="#{manager.delete(item)}"
                    styleClass="buttons" value="Delete" >
        <f:ajax execute="dataTable" render="dataTable" />
    </h:commandButton>
</h:dataTable>

Unrelated to the concrete problem, I suggest to use the attribute action instead of actionListener in the commandButton. See Differences between action and actionListener.

Upvotes: 1

Related Questions