Reputation: 8778
I have a Java Web 7 project in which I want to write JSF component values directly on an entity instance managed in a backing bean on every p:ajax
event. That works fine as long as I add a JSR 303 validation constraint, like @Size(min=5)
on one of the entity properties. The value is not longer set on the property with the constraint as long as the constraint is violated. The setter isn't called according to NetBeans debugger; the other property works just fine which leads me to believe that I've isolated the problem. I would like to perform validation when I want and thus get and invalid value set if I want. How to acchieve that?
In my minimal reproducible example I have the entity MyEntity
(JPA and JSR 303 annotations)
@Entity public class MyEntity implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue private Long id; @Size(min = 5) private String property1; private String property2; public MyEntity() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getProperty1() { return property1; } public void setProperty1(String property1) { this.property1 = property1; } public String getProperty2() { return property2; } public void setProperty2(String property2) { this.property2 = property2; } }
a JSF page
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<p:inputText value="#{myManagedBean.myEntity.property1}">
<p:ajax/>
</p:inputText>
<p:inputTextarea value="#{myManagedBean.myEntity.property2}">
<p:ajax/>
</p:inputTextarea>
<p:commandButton value="Save">
<p:ajax listener="#{myManagedBean.onSaveButtonClicked}"/>
</p:commandButton>
</h:form>
</h:body>
</html>
and a backing bean
@ManagedBean @SessionScoped public class MyManagedBean { private String value; private String textAreaValue; private MyEntity myEntity = new MyEntity(); public MyManagedBean() { } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getTextAreaValue() { return textAreaValue; } public MyEntity getMyEntity() { return myEntity; } public void setMyEntity(MyEntity myEntity) { this.myEntity = myEntity; } public void setTextAreaValue(String textAreaValue) { this.textAreaValue = textAreaValue; } public void onSaveButtonClicked(AjaxBehaviorEvent event) { System.out.println("value: "+value); System.out.println("textAreaValue: "+textAreaValue); System.out.println("myEntity: "+myEntity); } }
The example can be found at https://github.com/krichter722/jsf-skip-entity-validation. I'm using Primefaces 6.0.
Upvotes: 2
Views: 211
Reputation: 248
It is working as it is supposed to work. If the validation fails the setter shouldn't be called. If you want you can disable bean validations with this:
<f:validateBean disabled="true"/>
Upvotes: 2