h:inputText

HtmlInputText is a UIInput component that renders a field for editing single-line text.

Validation Usage

The value attribute can be validated via the validator attribute or by specifying an f:validator type of child tag. In addition, custom user feedback can be specified via the validatorMessage attribute.


Source Code

  1. <ui:composition xmlns="http://www.w3.org/1999/xhtml"
  2. xmlns:f="http://xmlns.jcp.org/jsf/core" xmlns:h="http://xmlns.jcp.org/jsf/html"
  3. xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
  4.  
  5. <h:form>
  6. <!-- Example 1: Validating an email address using the validator attribute. -->
  7. <h:inputText id="text" validator="#{inputTextBackingBean.emailAddressValidator}"
  8. validatorMessage="#{i18n['validator-message']}" value="#{inputTextModelBean.text}" />
  9. <h:message for="text" />
  10. <hr />
  11. <h:commandButton action="#{inputTextBackingBean.submit}" value="#{i18n['submit']}">
  12. <f:ajax execute="@form" render="@form" />
  13. </h:commandButton>
  14. <h:outputText id="modelValue" value="#{inputTextModelBean.text}" />
  15. </h:form>
  16.  
  17. <!-- Example 2: Validating an email address using an f:validateRegex child tag. -->
  18. <h:form>
  19. <h:inputText id="text" validatorMessage="#{i18n['validator-message']}"
  20. value="#{inputTextModelBean.text}">
  21. <f:validateRegex pattern=".+[@].+[.].+" />
  22. </h:inputText>
  23. <h:message for="text" />
  24. <hr />
  25. <h:commandButton action="#{inputTextBackingBean.submit}" value="#{i18n['submit']}">
  26. <f:ajax execute="@form" render="@form" />
  27. </h:commandButton>
  28. <h:outputText id="modelValue" value="#{inputTextModelBean.text}" />
  29. </h:form>
  30.  
  31. </ui:composition>
  1. @ManagedBean
  2. @RequestScoped
  3. public class InputTextModelBean {
  4.  
  5. private Date date = new GregorianCalendar().getTime();
  6. private String text;
  7.  
  8. public Date getDate() {
  9. return date;
  10. }
  11.  
  12. public String getText() {
  13. return text;
  14. }
  15.  
  16. public void setDate(Date date) {
  17. this.date = date;
  18. }
  19.  
  20. public void setText(String text) {
  21. this.text = text;
  22. }
  23. }
  1. @ManagedBean
  2. @RequestScoped
  3. public class InputTextBackingBean {
  4.  
  5. private static final Logger logger = LoggerFactory.getLogger(InputTextBackingBean.class);
  6.  
  7. @ManagedProperty(value = "#{inputTextModelBean}")
  8. private InputTextModelBean inputTextModelBean;
  9.  
  10. public void emailAddressValidator(FacesContext facesContext, UIComponent uiComponent, Object value)
  11. throws ValidatorException {
  12.  
  13. if (value != null) {
  14.  
  15. if (!value.toString().matches(".+[@].+[.].+")) {
  16. FacesMessage facesMessage = new FacesMessage();
  17. facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
  18. throw new ValidatorException(facesMessage);
  19. }
  20. }
  21. }
  22.  
  23. public void setInputTextModelBean(InputTextModelBean inputTextModelBean) {
  24. this.inputTextModelBean = inputTextModelBean;
  25. }
  26.  
  27. public void submit() {
  28.  
  29. Object value = inputTextModelBean.getText();
  30.  
  31. if (value == null) {
  32. value = inputTextModelBean.getDate();
  33. }
  34.  
  35. logger.info("You entered: " + value);
  36. }
  37.  
  38. public void valueChangeListener(ValueChangeEvent valueChangeEvent) {
  39.  
  40. FacesContext facesContext = FacesContext.getCurrentInstance();
  41. PhaseId phaseId = facesContext.getCurrentPhaseId();
  42. logger.debug("valueChangeListener: phaseId=[{0}]", phaseId.toString());
  43.  
  44. String phaseName = phaseId.toString();
  45. FacesMessage facesMessage = new FacesMessage("The valueChangeListener method was called during the " +
  46. phaseName + " phase of the JSF lifecycle.");
  47. facesContext.addMessage(null, facesMessage);
  48. }
  49. }
Liferay Faces Bridge Implementation 5.0.0 + Showcase Common 3.1.1 + Liferay Faces Util 3.4.1 + Mojarra 2.2.20