declarative input validation with jsr 303 and extval
DESCRIPTION
Slides of my session at J-Fall 2010TRANSCRIPT
![Page 1: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/1.jpg)
WWW.TRANSFER-SOLUTIONS.COM
SPREKER :
E-MAIL :
DATUM :
Declarative input validationwith JSR 303 and ExtVal
BART KUMMEL
3 NOVEMBER 2010
![Page 2: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/2.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 2
Who am I?
Bart KummelNearly 10 years experience in software development
Of which 5 years in Java EE
Consultant @ Transfer SolutionsCompetence Manager Java EE @ Transfer SolutionsAuthor of Apache MyFaces 1.2 Web Application Development
See http://tinyurl.com/am12wad
![Page 3: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/3.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 3Photo: Salar de Uyuni, Bolivia; © 2010 by Bart KummelPhoto: Salar de Uyuni, Bolivia; © 2010 by Bart Kummel
![Page 4: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/4.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 4
Don’t Repeat Yourself
Less code = less bugs
Duplicated code = duplicated bugs
Duplicated code = duplicated maintenance
Dupliacted maintenance = forgotten maintenance
![Page 5: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/5.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 5
DRY violations in classic Java EE apps
Validation is programmed in Model beansBecause that’s where it belongs
Validation is repeated in View layerBecause you have to use JSF Validators
Validation is even repeated multiple times in the ViewBecause the same bean is used in multiple JSF pages
![Page 6: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/6.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 6
Remove validation code from ViewLet View generate validation based on Model
Let’s fix this
How to fix it?
That’s why Bean Validation (JSR 303) was created
![Page 7: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/7.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 7
JSR 303: the idea
Standardized way to express validation constraints
Any UI technology can interpret those constraints and enforce them
Non-UI technologies can also use the validation information
![Page 8: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/8.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 8
JSR 303: the idea implemented
JSR 303 is part of Java EE 6The reference implementation is Hibernate Validator 4.*
See http://hibernate.org/subprojects/validator.htmlHibernate Validator 4.* can also be used in Java EE 5
A JSR 303 implementation is only the way to express the validation constraints
You don’t get UI validation logic if the UI framework doesn’t support JSR 303
![Page 9: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/9.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 9
Bean Validation in Java EE 5
Add Hibernate Validator 4.* as library...and some extra libraries, provided in the Hibernate Validator package
Use JSR 303 annotations in your beans
Use MyFaces ExtVal 1.2.* to add declarative validation support to JSF 1.2
![Page 10: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/10.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 10
Bean Validation in Java EE 6
No need to add a JSR 303 implementationJSR 303 is part of the Java EE 6 platform
Use JSR 303 annotations in your beansJSF 2.0 has support for JSR 303 annotations out of the box
But support is limited
You can (and should!) still use ExtVal (2.0.*) and get lots of benefits (more on that later)
![Page 11: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/11.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 11
Side note: ExtVal versioning
There are three current versions of ExtVal1.1.* for JSF 1.11.2.* for JSF 1.22.0.* for JSF 2.0
The latest stable release is release 3That is: 1.1.3, 1.2.3 and 2.0.3
Lots of exciting new stuff is going into the next version
Snapshot releases of ExtVal are very high quality
![Page 12: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/12.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 12
Example: classic validation code in bean
@Min(0)@Max(100000)
private int capacity;
public void setCapacity(int capacity) { if(capacity >= 0 && capacity <= 100000) { this.capacity = capacity; } else { // throw exception }
}
![Page 13: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/13.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 13
Example: JSR 303 annotations
@Min(0)@Max(100000)
private int capacity;
public void setCapacity(int capacity) { this.capacity = capacity;}
Extra benefits:
– less code
– better readable
![Page 14: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/14.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 14
Example: classic validation in JSF page
<h:inputText value="#{room.capacity}" > <f:validateLongRange minimum = "0" maximum = "100000" /></h:inputText>
![Page 15: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/15.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 15
Example: no validation in JSF page!
<h:inputText value="#{room.capacity}" />
Benefits:
– less code
– DRY!
![Page 16: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/16.jpg)
16
WWW.TRANSFER-SOLUTIONS.COM
Demo 1: Bean Validation basics in Java EE 6
![Page 17: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/17.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 17
So why do we need ExtVal?
To use Bean Validation in Java EE 5 / JSF 1.2
To have advanced options in Java EE 6
![Page 18: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/18.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 18
ExtVal on Java EE 6: advanced options
Cross validationViolation severity
i.o.w. give warnings instead of errors
More flexibility in choice of annotations to useJSR 303, JPA, ExtVal, own annotation or any combination
Customization on all levels, e.g.:Custom message resolversCustom validation strategiesCustom meta data
demos coming up!
![Page 19: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/19.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 19
Configuring ExtVal
Just add the ExtVal .jar files to your project
![Page 20: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/20.jpg)
20
WWW.TRANSFER-SOLUTIONS.COM
Demo 2: Adding the ExtVal .jar files to our project
![Page 21: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/21.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 21
Cross validation
Examples of cross validationcheck if two values are equalcheck if date is before or after other datevalue is only required if other value is empty (or not)etcetera...
![Page 22: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/22.jpg)
22
WWW.TRANSFER-SOLUTIONS.COM
Demo 3: Cross validation
![Page 23: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/23.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 23
Demo 3 – Summary
@DateIs can be used for date-related cross validations
Use DateIsType.before, DateIsType.after or DateIsType.same
Other cross validation annotations:@Equals and @NotEquals for equality-based cross validation of any type@RequiredIf for conditional required fields
Use RequiredIfType.empty or RequiredIfType.not_empty
![Page 24: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/24.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 24
Violation severity
Give certain validation rules a severity level of “warning”
A warning will be given to the user, but “invalid” data can be submitted
![Page 25: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/25.jpg)
25
WWW.TRANSFER-SOLUTIONS.COM
Demo 4: Setting violation severity to “warning”
![Page 26: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/26.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 26
Demo 4 – summary
Violation severity is not part of the JSR 303 standardWe use payload to add violation severity level as custom meta data
JPA also interprets JSR 303 contraints before persisting data, but does not recognise violation severity
Solution: use ExtVal annotations instead
![Page 27: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/27.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 27
Customization on all levels
ExtVal is full of customization hooks
A lot of ready-made add-ons are availablesee http://os890.blogspot.com
![Page 28: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/28.jpg)
28
WWW.TRANSFER-SOLUTIONS.COM
Demo 5: Creating a custom annotation and a custom validation strategy
![Page 29: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/29.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 29
Demo 5 – summary
Technically, creating a custom annotation is not an ExtVal feature
It is just a Java feature
We need an ExtVal validation strategy to make a custom annotation workWe need to map our annotation to our validation strategy
We can create a startup listener for thisAs an alternative we can use ExtVal plugins to use alternative ways of configuration
![Page 30: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/30.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 30
Summary
With annotation based valition, we can finally create DRY JSF applicationsExtVal gives us the opportunity to use annotation-based validation on Java EE 5 On Java EE 6, ExtVal gives us:
More powerful annotation-based validationMore flexibility
![Page 31: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/31.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 31
More info...
I will put links to slides & demo code on my blog http://www.bartkummel.net
Chapter 10 of MyFaces 1.2 Web Application Development
http://tinyurl.com/am12wad
MyFaces ExtVal: http://myfaces.apache.org/extensions/validatorhttp://os890.blogspot.com/
![Page 32: Declarative input validation with JSR 303 and ExtVal](https://reader033.vdocuments.mx/reader033/viewer/2022042606/5496cce1b47959794d8b50e6/html5/thumbnails/32.jpg)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 32
&Q u e s t i o n sA n s w e r s
CONSULTING | MANAGED SERVICES | EDUCATION
WWW.TRANSFER-SOLUTIONS.COM