83605

JSF validation pattern for input control

Question:

I have a JSF page and I have a inputtext in this page. I take the user keywords form this inputtext. My problem is that I want to control the number of keywords that the user enter(they separated by "-" ) and check them to not to be more than a specific number. is there any validation pattern or any better way to check them?

Answer1:

You can do this validation by using a <a href="http://www.mkyong.com/jsf2/custom-validator-in-jsf-2-0/" rel="nofollow">Custom Validator</a>, by defining a java class implementing Validator interface, which should performs the specific desired validation. Here's a small example that shows when the user types keywords (separated by "-") more than a predetermined max limit (e.g 5), a validation message pops up when passing to the next field :

<strong>View :</strong> index.xhtml

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>Custom Validator Example</title> </h:head> <h:body> <h:form> <h:panelGrid columns="3"> Give strings separated by "-" : <h:inputText id="input" size="30" required="true" requiredMessage="Field required" value="#{myBean.myExpression}"> <f:ajax event="blur" render="inputMessage" /> <f:validator binding="#{myValidator}" /> </h:inputText> <h:message for="input" id="inputMessage" style="color:red" /> Enter your name: <h:inputText id="input1" required="true" requiredMessage="missed name" value="#{myBean.name}" /> </h:panelGrid> </h:form> </h:body>

<strong>A simple managed-bean :</strong> MyBean.java

// imports @ManagedBean @RequestScoped public class MyBean implements Serializable { private String myExpression ; private String name ; // getters/setters }

<strong>Custom Validator :</strong> MyValidator.java

// imports @ManagedBean @ViewScoped public class MyValidator implements Validator { @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { int max_limit = 5; // for example String expression = (String) value; String[] result = expression.split("-"); int count=0; for(int i=0;i<result.length;i++){ if (result[i].equals("")) continue; // Empty keywords (e.g successive "-" caracters) are ignored. Else, just omit this line count++; } if (count > max_limit) { FacesMessage facesmsg = new FacesMessage(FacesMessage.SEVERITY_ERROR,"Exeeded allowed limit !",null); throw new ValidatorException(facesmsg); } } }

Recommend

  • Updating ASP.NET UpdatePanel during processing loop
  • PHP Session check not working when i redirect from another page
  • ModelAndView object not returned to jsp
  • Login form issue with spring security 2.0.7 and spring 2.5
  • Dynamic Event Handler not Firing
  • a:first-child modifying all links within my list
  • :hover CSS is not working?
  • How to forcefully download .csv file instead of getting in open on browser in html
  • Using target on div without a href
  • Overriding window.location on Webbrowser Control
  • Declaration of does not work with Ionic's ion-nav-view in IBM MobileFirst
  • Is it possible to extract table infomation using Apache Tika?
  • Why is this button causing my layout to break?
  • Hibernate - Cannot connect to DB
  • What is the best way to include a style sheet for a specific page?
  • How do I include screenshots of the full page in my serenity report (and not only of the viewport) u
  • ng-repeat not working with table but works with list
  • How to add regEx in angular filter
  • JSF2.0 + Primefaces 3.0.1 + jquery 1.6.4 + p:commandLink + IE8 throws Unexpected call to method or p
  • How can I include If-None-Match header in HttpRequestMessage
  • Bootstrap (v3.3.4) glyphicons not displayed in IE when refresh page (F5)
  • Parse a date string in a specific locale (not timezone!)
  • Play WS (2.2.1): post/put large request
  • Display issues when we change from one jquery mobile page to another in firefox
  • Deserializing XML into class C#
  • Do I've to free mysql result after storing it?
  • Python - Map / Reduce - How do I read JSON specific field in using DISCO count words example
  • Warning: Can't call setState (or forceUpdate) on an unmounted component
  • Is there a mandatory requirement to switch app.yaml?
  • bootstrap to use multiple ng-app
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • JTable with a ScrollPane misbehaving
  • Turn off referential integrity in Derby? is it possible?
  • unknown Exception android
  • JaxB to read class hierarchy
  • How to Embed XSL into XML
  • failed to connect to specific WiFi in android programmatically
  • How can I use threading to 'tick' a timer to be accessed by other threads?