How can I skip bean validation if all fields for the entity are empty?

bean-validation, jsf-2, omnifaces

Solution

You can just use EL in `disabled` attribute of `<f:validateBean>` (and `<o:validateBean>`). You can use EL to check if any of those fields have been filled. You can use `binding` attribute to reference a component in EL. You can check if a field has been filled by checking the value of the request parameter associated with component's client ID.

Then, to disable bean validation on those fields when `#{addressIsEmpty}` evaluates `true`, it's easier to use `<f:validateBean>` instead of `<o:validateBean>` as the former can be used to wrap multiple input components.

So, all with all, this should do:

<c:set var="addressIsEmpty" value="#{empty param[street.clientId] and empty param[nr.clientId] and empty param[city.clientId]}" />

<f:validateBean disabled="#{addressIsEmpty}">
    <h:inputText binding="#{street}" value="#{bean.address.street}" />
    <h:inputText binding="#{nr}" value="#{bean.address.nr}" />
    <h:inputText binding="#{city}" value="#{bean.address.city}" />
</f:validateBean>

The `<o:validateBean>` is only easier to use if you intend to control it on a per-input or per-command basis.

Problem

I have a address entity like this: ``` @Entity public class Address implements java.io.Serializable { @Id @GeneratedValue(strategy = IDENTITY) private Long id; @Size(max = 100) private String street; @Size(max = 15) private String nr; @Field @Size(min = 1, max = 20) @NotBlank private String city; } ``` This is part of several other entities. For one such entity the address is optional. I have a view where our users can edit the whole entity via several form inputs, including several address fields. To be able to edit the address, I initialize the parent entity with an empty address entity. Now the problem is: I don't want to persist an `Address` on the entity when all address fields are empty. For that to work, I need to skip validation for the entity if (and only if) all fields of `Address` are empty. How do I do that in the most elegant way? What I'm currently doing is to skip bean validation altogether with `<o:validateBean disabled="true" />` for the submit button, setting the address to `null` if it comes out empty. But this sounds like a total hack as it disables all validation in all cases. How can I do better?

Original source