kauedg
kauedg

Reputation: 817

Avoid validation of @Size annotation in JPA

I have a JPA entity with the following attribute:

public class Person implements Serializabe {
...
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 45)
@Column(name = "name", nullable = false, length = 45)
private String name;
...

And it's view:

<h:outputLabel value="Name: "/>           
<h:inputText id="name"
             value="#{personBean.person.name}"
             validator="#{personValidator.validate}"/>
<rich:message for="name"/>

The personValidator checks if 0 < name.length < 45 and if it already exists in the database; it works fine. When I try do add an empty name, or bigger than 45 chars, I get my correct validator message but in the Glassfish output is shown:

INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
sourceId=contentForm:name[severity=(ERROR 2), summary=(size must be between 1 and 45), detail=(size must be between 1 and 45)]

I have tried adding <validation-mode>NONE</validation-mode> to my persistence.xml but no effect. How do I avoid this @Sizeannotation validation?

Upvotes: 3

Views: 6616

Answers (2)

Arjan Tijms
Arjan Tijms

Reputation: 38163

The message you're seeing is because a faces message (likely originating from a validator) has been emitted, but likely has not been displayed.

In this case, the @Size annotation is from Bean Validation and is processed by both JPA and JSF.

Unless you manually generated the message from your backing bean (by e.g. putting a try/catch around your business logic) this one is really coming from JSF and disabling anything in JPA's persistence.xml won't make a difference.

You can disable the JSF validation by nesting an f:validateBean tag inside your input components and setting the disabled attribute to true. See Temoporarily suppress beanvalidation with JSF

Upvotes: 4

Aviram Segal
Aviram Segal

Reputation: 11120

You can disable the validation for the specific input by adding a validateBean tag:

<h:outputLabel value="Name: "/>           
<h:inputText id="name"
             value="#{personBean.person.name}"
             validator="#{personValidator.validate}">
    <f:validateBean disabled="true"/>
</h:inputText>
<rich:message for="name"/>

Upvotes: 2

Related Questions