model-based constraints defined using Hibernate Validator

UI Components for JSF
samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

24 Mar 2011, 08:27

Hello,

Can anyone tell me how to use model-based constraints defined using Hibernate Validator for Primefaces?

Thanks
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

24 Mar 2011, 10:09

If you are talking about Bean Validation (JSR 303) this can be applied using <f:validateBean />

Examples:

Code: Select all

<p:inputText value="#{managedBean.firstName}">
    <f:validateBean />
</p:inputText>

Code: Select all

<f:validateBean>
    <p:inputText value="#{managedBean.firstName}" />
    <p:inputText value="#{managedBean.middleName}" />
    <p:inputText value="#{managedBean.lastName}" />
</f:validateBean>
This also works with validation groups:

Code: Select all

<f:validateBean validationGroups="com.example.jsf.TextValidationGroup">
    <p:inputText value="#{managedBean.firstName}" />
    <p:inputText value="#{managedBean.middleName}" />
    <p:inputText value="#{managedBean.lastName}" />
</f:validateBean>

samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

24 Mar 2011, 10:43

Thank you for the response with examples.

How to apply constraint with primedaces?
Eg.
@TeadingRuleConstraints()
private StockTrades storkTrades;

I want its validation message to be printed out in the jsf message tag eg. <p: messages />

Thanks a lot
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

robert.m
Posts: 226
Joined: 07 Dec 2010, 22:52
Location: Salzburg/Austria

24 Mar 2011, 13:32

I don't exactly know what you want, but I guess you need to define your own Constraints:
http://musingsofaprogrammingaddict.blog ... -bean.html

In your validator-class, you can generate new FacesMessages and add it to the FacesContext, then they get rendered by the p:messages component

Then you can still use the f:validateBean-Component to trigger the bean validation

samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

24 Mar 2011, 13:42

Yes, this is exactly what I mean. Thank you for the reference link. I will try this out .

Thanks very much
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

User avatar
bumble.bee
Posts: 723
Joined: 29 Sep 2010, 21:39
Location: United States

24 Mar 2011, 14:56

If you annotate your entities I don't think you need to use the f:validateBean component at all.

samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

25 Mar 2011, 12:59

Then how does constraint annotated validation work with PF?

Best regards
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

tandraschko
PrimeFaces Core Developer
Posts: 3979
Joined: 03 Dec 2010, 14:11
Location: Bavaria, DE
Contact:

25 Mar 2011, 13:16

It's not PF specific.

Add a BV implementation to your JSF2 project and it's working.

Add context param:

Code: Select all

<context-param>
    <param-name>javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL</param-name>
    <param-value>true</param-value>
</context-param>
Bean:

Code: Select all

public Class UserBean {
@NotNull
@Size(min = 5, max = 20)
private String name;
Page:

Code: Select all

   <h:inputText id="username" value="#{userBean.name}" />
Thomas Andraschko

PrimeFaces | PrimeFaces Extensions

Apache Member | OpenWebBeans, DeltaSpike, MyFaces, BVal, TomEE

Sponsor me: https://github.com/sponsors/tandraschko
Blog: http://tandraschko.blogspot.de/
Twitter: https://twitter.com/TAndraschko

samwun9988
Posts: 69
Joined: 22 Jan 2011, 11:04

25 Mar 2011, 14:03

If not PF specific, I know how to use it.
It doesn;'t require ANY xml file in general. I am with EJB3 - dependancy injection.

In fact, I just feel a bit difficult to use Constraint validation with PF.

Thanks
Sam
Primefaces 2.2.1, JBOSS 6.0.0.Final, JDK1.6

Franke
Posts: 49
Joined: 12 Oct 2010, 08:41

25 Mar 2011, 14:31

As said, PF has nothing to do with bean validation, and it could not get easier. Simply annotate your entites, and all error messages will appear at p:messages (or h:messages)
Using PF 3.0 with default JSF implementation of JBoss 6

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 23 guests