File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes programmatically add custom validators Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "programmatically add custom validators" Watch "programmatically add custom validators" New topic
Author

programmatically add custom validators

Leonardo Shikida
Greenhorn

Joined: Jan 02, 2003
Posts: 29
Hi

I can find several tutorial about how to write your own validator.

But none of them explains how to create a custom validator and add it programmatically

For example, LengthValidator is a default JSF validator and it´s added programmatically just like this

LengthValidator lengthValidator = (LengthValidator) application.createValidator(LengthValidator.VALIDATOR_ID);
lengthValidator.setMinimum(2);
someOtherComponent.addValidator(lengthValidator);


where

public static final java.lang.String VALIDATOR_ID = "javax.faces.Length";


I´ve tried writing my custom validator and assigning a unique id in faces-config.xml, when I try to add it to another component, I get

09:17:01,803 ERROR [MyBeanMB] Expression Error: Named Object: MyCustomValidator not found.
javax.faces.FacesException: Expression Error: Named Object: my.company.MyCustomValidator not found.
at com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:959)

Is there any tutorial anywhere about this? How do I write validators that can be added programmatically?

TIA

Leo K.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Hi
I tested your code and works fine for me! Using MyFaces 2.0.2.

For example I have a custom validator like thisand make a validator programmatically like you did i.e.

What version of JSF are you using? Do you define the name of your validator in faces-config.xml or (like I did above) with @FacesValidator annotation?
Saurabh Saxen
Greenhorn

Joined: Aug 15, 2011
Posts: 1
Ilari Moilanen,

Do you have a fully working example project that you can attach please!

Thanks,
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15662
    
  15

I don't understand the value of dynamically adding a validator myself. Unless you're HEAVILY into dynamic View construction, it's generally easier to just define the thing statically in faces-config.xml.

Your error message would appear to indicate that things were mostly done correctly, except that you didn't include the file /WEB-INF/classes/my/company/MyCustomValidator.class in your WAR. Or its equivalent.


Customer surveys are for companies who didn't pay proper attention to begin with.
Ilari Moilanen
Ranch Hand

Joined: Apr 15, 2008
Posts: 198
Saurabh Saxen,

alas I do not have an example. All I have is code in production use.

But there should be nothing complicated in using the custom validator. As I said above I use JSF 2 (MyFaces) and the above code works as is. I do NOT have anything in the config file(s) since the @FacesValidator annotation above is enough. The validate method throws javax.faces.validator.ValidatorException if the validation fails. You can google the different classes used to find what their API says about them.

I do not use the validator programmatically anywhere, I just tested it because the original poster said that his code does not work. Instead I use the validator in UI like this:

(where t: is defined as xmlns:t="http://myfaces.apache.org/tomahawk")
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: programmatically add custom validators
 
Similar Threads
which is best, stuts validator or javascript
"Struts 2 Design and Programming: A Tutorial" Validation Question
Dynamic Field Validation
Custom error messages in JSF validators?
validateLongRange in JSF