Setting validator attribute using ui based EL: repeat var

I am looking for some guidance today with the problem I am facing.

What I'm trying to do is build a page on the fly with validation and all. The end result is to allow the user to customize the fields on the page using administrative functions. Below is a copy of the code that I use as a test page, where I go through the "Configure" fields and write out the fields using certain criteria.

<ui:repeat var="field" value="#{eventMgmt.eventFields}" varStatus="status">
  <div class="formLabel">
    <h:outputLabel value="#{field.customName}:"></h:outputLabel>
  </div>
  <div class="formInput">
    <h:inputText id="inputField" style="width:# {field.fieldSize gt 0 ? field.fieldSize : 140}px;">
      <f:validateRegex  disabled="#{empty field.validationPattern}" pattern="#{field.validationPattern}"></f:validateRegex>
    </h:inputText>
    <h:message for="inputField" showDetail="true" errorClass="errorText"></h:message>
  </div>
</ui:repeat>

After the page is displayed and I try to pass any values ​​for the field, I get the following message: "The Regex template must be set to a non-empty value." which, obviously, means that the expression is empty. I am wondering that fields that do not have an expression for them will be disabled when EL is evaluated. I can also take the same code # {field.validationPattern} and put it on the page, and the correct value will be written on the page.

So my questions are: 1. Is this possible? 2. At what point does the JSF container look at the template binding for the validate regular expression? 3. What am I doing wrong or What is the right way to do this?

I am running Tomcat 7.0.22, Mojarra 2.1.5 and Eclipse as my IDE.

+5
source share
1 answer

<f:validateRegex>, <ui:repeat>.

<f:xxx> , . , . EL . <h:xxx> <ui:xxx>, <ui:repeat>, . EL .

, , <f:validateRegex> , #{field} EL null.

.

  • , Field, :

    <h:inputText ... validator="#{field.validate}" />
    

    Field, :

    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        if (pattern != null) {
            RegexValidator regexValidator = new RegexValidator();
            regexValidator.setPattern(pattern);
            regexValidator.validate(context, component, value);
        }
    }
    

  • #{eventMgmt.eventFields} ListDataModel<Field> #{eventMgmt} bean. , :

    <h:inputText ... validator="#{eventMgmt.validate}" />
    

    bean #{eventMgmt}:

    private DataModel<Field> model;
    private RegexValidator regexValidator;
    
    @PostConstruct
    public void init() {
        regexValidator = new RegexValidator();
    }
    
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        String pattern = model.getRowData().getPattern();
    
        if (pattern != null) {
            regexValidator.setPattern(pattern);
            regexValidator.validate(context, component, value);
        }
    }
    

  • Validator, RegexValidator <f:attribute>, Validator. <f:attribute> ValueExpression, . :.

    <h:inputText ...>
        <f:validator validatorId="extendedRegexValidator" />
        <f:attribute name="pattern" value="#{field.pattern}" />
    </h:inputText>
    

    @FacesValidator("extendedRegexValidator")
    public class ExtendedRegexValidator extends RegexValidator {
    
        @Override
        public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
            String pattern = (String) component.getAttributes().get("pattern");
    
            if (pattern != null) {
                setPattern(pattern);
                super.validate(context, component, value);
            }
        }
    
    }
    

  • , JSF OmniFaces, <o:validator>. .

    <h:inputText ...>
        <o:validator validatorId="javax.faces.RegularExpression" pattern="#{field.pattern}" />
    </h:inputText>
    

    , . <o:validator> , .

. :

+11

All Articles