JSF: a regular expression validator
Continuing my example from a few days back, this example shows how to create a JSF validator tag which checks agains a regular expression.
Step 5: custom validator tag
- Add validator class
package jsftest1;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
public class RegExValidator implements
Validator {
String regex;
public void validate(
FacesContext _context,
UIComponent _component,
Object _value)
throws ValidatorException {
String val = (String) _value;
if (!val.matches(regex)) {
throw new ValidatorException(
new FacesMessage(
"Incorrect value, does not match regular expression: "
+ regex)); }
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
}
}
- Add validator tag class
package jsftest1;
import javax.faces.validator.Validator;
import javax.faces.webapp.ValidatorTag;
import javax.servlet.jsp.JspException;
public class RegExValidatorTag extends
ValidatorTag {
private String regex;
public RegExValidatorTag(){
super();
super.setValidatorId("regexValidator");
}
protected Validator createValidator()
throws JspException {
RegExValidator result = null;
result = (RegExValidator)super.createValidator();
result.setRegex(regex);
return result;
}
public String getRegex() {
return regex;
}
public void setRegex(String regex) {
this.regex = regex;
}
}
- Add tag lib descriptor file
<PROJECT_HOME>/web/WEB-INF/jsftest1.tld
<taglib>
<tlibversion>0.1</tlibversion>
<jspversion>2.0</jspversion>
<shortname>JSF test 1 taglib</shortname>
<tag>
<name>regexValidator</name>
<tagclass>jsftest1.RegExValidatorTag</tagclass>
<attribute>
<name>regex</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
- Add validator to faces-config.xml
<validator>
<validator-id>regexValidator</validator-id>
<validator-class>jsftest1.RegExValidator</validator-class>
<attribute>
<attribute-name>regex</attribute-name>
<attribute-class>java.lang.String</attribute-class>
</attribute>
</validator>
- Modify jsp page
<h:outputLabel for="firstName">
<h:outputText value="#{bundle.firstName}"/>
</h:outputLabel>
<h:inputText id="firstName" value="#{registerBean.firstName}" >
<f:validateLength minimum="1"/>
style="font-weight: bold;"><j:regexValidator regex="[A-Z]+"/>
<f:validator validatorId="registerValidator"/>
</h:inputText>
<h:message for="firstName"/>
25 Mar 2004 |
March 26th, 2004 at 3:46 pm
Should a web page designer decide how a form gets validated?
March 26th, 2004 at 11:45 pm
Probably non the web page designer, but the web page programmer should. In my experience, the designer usually doesn’t create the actual pages, he only creates a template that shows how to achieve the correct layout. The programmers take these templates as examples and program the pages. I guess it depends on the project, and the people on the project.
November 17th, 2004 at 10:12 pm
I have the problem that when i use the tag, it looks like JSF creates two RegExValidator objects that, the first one has the attribute regex value, but the second one, which actually is used for the validate method, has its regex property in null!
I dont know!
November 21st, 2004 at 7:34 pm
harry, a simple solution would be to enable value-binding on the regex property.
May 2nd, 2005 at 10:53 pm
I have the problem that when i use the tag, it looks like JSF creates two RegExValidator objects that, the first one has the attribute regex value, but the second one, which actually is used for the validate method, has its regex property in null!
I dont know!
May 12th, 2005 at 4:40 pm
This is a super example!!! Will it work for just about any regex that I pass from the jsp?
And when you add this tag:
What library are you calling at the the top of your page to tell the system what “j” means?
(i.e. < %@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
< %@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>)
Would this just be < %@ taglib uri="../../WEB-INF/test1.tld prefix="j" %> or whatever corresponds to the path for test1.tld?
July 6th, 2005 at 5:52 pm
Hi
Many thanks for your tutorial, i am finding it a good learning platform, however Ihave the problem that when i use the tag, it creates two RegExValidator objects, the first one has the attribute regex value(as definded in the JSP), but the second one, which actually is used for the validate method, has its regex property in null! This gives me a NULL pointer exception when i try and run the app.
Why is this and how to i resolve it ?
Note : I also had to add to the jsp page, i hope that this is correct
Thanks very much for you help
July 17th, 2005 at 10:15 pm
I’ve just recreated the complete example in jdeveloper (version 10.1.3). It works for me, but maybe it’s a bug in the jsf implementation? What jsp container en jsf implementation are you using?
Andrej
July 27th, 2005 at 6:28 pm
Hi
Many thanks for your reply, i am using JBOSS as my webServer and the JSF implementation.
THanks
July 28th, 2006 at 10:09 pm
I had the same problem with an extra RegExValidator being created but with null properties (and hence an NPE during validation).
It turned out that the state of the validator wasn’t being restored fully during the “restore view” phase.
Everything got fixed by implementing StateHolder (see http://java.sun.com/j2ee/1.4/docs/tutorial/doc/JSFDevelop6.html)
I saw this using MyFaces (1.1.3) for my JSF implementation. Don’t know if it would’ve worked as is with Sun’s RI.
December 3rd, 2006 at 8:46 am
I am facing a problem. I changed taskManagerContainer.jsp page like
i put like runatclent=’true’ onclick = ‘showmessage()’
function showMessage()
{
alert(“Forwarded….”);
document.Forms0.submit();
}
After I restart the tomcat server I am getting the error like this. I think its the error from the tld file. Is int it?. If its how can I fix it?.
Error:
FormProcessor: can not perform ‘doFreshInclude’ : /custom/workflow/taskManagerContainer.jsp(63,0) Attribute runatclient invalid for tag actionbutton according to TLD
/custom/workflow/taskManagerContainer.jsp(63,0) Attribute runatclient invalid for tag actionbutton according to TLD
Any body has any ida, thanks advance. Appreciated
August 3rd, 2007 at 3:46 pm
Hi drkeoni,
I Implemented StateHolder But I am still having the same problem extra RegExValidator being created but with null properties. Can you Please publish your code here.
Thanks
August 17th, 2007 at 4:07 pm
The problem of having to instanciated validators (one with the regexp and the other without) comes from that one validator is instanciated by the taglib j:regexValidator and the other is instanciated by the tag lib f:validator.
In fact when you define your own taglib to instanciate a parametered validator, you don’t need to use the f:validator tag:lib. Your specific taglib is enough.
Hope it helps
October 3rd, 2007 at 2:02 pm
Hi
I do all like example. But i haw sum problem.
Wen fest go to jsp page initialized RegExValidator with regex = [A-Za-z]+ but wen i input data in RegExValidator regex = null
and throw java.lang.NullPointerException
Where my error? Please help my. (Ans sorry for my English)
October 4th, 2007 at 8:09 am
Thank you for this example!
In my problem need realize StateHolder interface and all work reasonably. good luck!!