Note that there are some explanatory texts on larger screens.

plurals
  1. POValidate arbitrary objects in JSF?
    text
    copied!<p>So, you can validate input using JSR-303 annotations on the binding bean property:</p> <pre><code>class Ticket { @MinAge(18) Person person; } class Person { @Min(1) @Max(100) int age; } &lt;p:inputText id="age" value="#{bean.ticket.person.age}" /&gt; </code></pre> <p>Here, the property Person.age is validated (between 1..100) with no problem.</p> <p>The problem is, I want to validate the outer instance (person.age >= 18). But how to make the property <code>bean.ticket.person</code> be known to validation?</p> <p>I want something like:</p> <pre><code>&lt;p:inputText id="age" value="#{bean.ticket.person.age}"&gt; &lt;f:validate value="#{bean.ticket.person}" /&gt; &lt;/p:inputText&gt; </code></pre> <p>Or:</p> <pre><code>&lt;p:inputText id="age" value="#{bean.ticket.person.age}"&gt; &lt;f:validator id="jsr303-validator" value="#{bean.ticket.person}" /&gt; &lt;/p:inputText&gt; </code></pre> <p>The problem is, I can't pass a value to <code>&lt;f:validator /&gt;</code>. I want to add extra properties to the validation process, more then only the inputs appeared on the page.</p> <p><strong>P.S.</strong> This is a simplified example, the real application is:</p> <pre><code>... &lt;p:inputText id="principalLabel" value="${activeACL.principal.label}" readonly="true" /&gt; &lt;p:commandButton value="Choose..." onclick="choosePrincipalDialog.show()" /&gt; ... &lt;p:commandButton value="Save" action="${bean.saveACL}" oncomplete="editACLDialog.hide()" update="index" /&gt; </code></pre> <p>And activeACL of type ACL_DTO:</p> <pre><code>class ACL_DTO { ... @IdRequired Principal_DTO principal; } </code></pre> <p>Here, <code>choosePrincipalDialog</code>'s actionListener will implicit change the <code>${activeACL.principal.id}</code>, which is initially <code>null</code>. IdRequired is a custom constraint which constrains an object's id member property is not null or -1.</p> <p>Though, I can change to use @NotNull on the id property, and add a hidden-input to enable the validation on the id:</p> <pre><code>class Principal_DTO { ... @NotNull @Min(0) Long id; } ... &lt;h:inputHidden id="principalId" value="${activeACL.principal.id}" /&gt; &lt;h:inputText id="principalLabel" ... </code></pre> <p>But, in this way I can't reuse the validation messages any more. Give message like "This string should not be null", "This value should not be -1" to the user seems meaningless.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload