2

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.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
  • Hm, maybe if you add an event to p:ajax? According to the documentation, the default "is defined by parent ClientBehaviorHolder component the behavior is attached to", whatever the hell that would be... but I think this default changes, when you actually add the validation annotation. Maybe. (Nobody understands the jsf life cycle thing anyway). – slowy Apr 19 '17 at 17:01
  • Then my name is nobody – Kukeltje Apr 19 '17 at 18:08
  • 2
    Possible duplicate of [JSF 2.0: How to skip JSR-303 bean validation?](http://stackoverflow.com/questions/4852496/jsf-2-0-how-to-skip-jsr-303-bean-validation) – Kukeltje Apr 19 '17 at 18:12

1 Answers1

2

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"/>
David Mantilla
  • 228
  • 1
  • 10
  • 1
    That is exactly what is in (one of) the answers in the duplicate... Please mark the question as such – Kukeltje Apr 20 '17 at 06:56