hugri
hugri

Reputation: 1416

What's the best way to create parametrized multi-purpose custom facelet tags

I created a Facelet tag for rendering a textinput with a label. That's very helpful when for avoiding repeating the same code over and over again.

Though I'm struggling with handling different use cases within this single tab (date vs. text, required vs. not required, textarea vs. normal text etc.)

I ended up with having multiple tags within my component all with a more or less complicated rendered attribute, like shown here:

    <h:inputText    
            onblur="makeNotEmpty(this)"
            onfocus="makeNotEmptyFocus(this)"
            id="#{cid}"
            value="#{value}"
            rendered="#{textarea!='true' and type!='email' and notrequired!='true' and nullablenumber!='true'}"
            style="#{style }"   
            required="true"
            disabled="#{disabled }">        
            <f:validator validatorId="notnull"/>    

    </h:inputText> 
    <h:inputText                    
        onblur="    
            makeNotEmpty(this)"
        onfocus="makeNotEmptyFocus(this)"
        id="#{cid}"
        value="#{value}"
        rendered="#{type=='email'}"
        style="#{style }"
        required="true"
        disabled="#{disabled }">            
        <f:validator validatorId="email" />
        <f:validator validatorId="notnull"/>
    </h:inputText> 

Of course that's no optimal and rather tedious to write. One other problem I think is that using this approach I have multiple components with the same ID in the component tree (I'm not sure if that's a problem as only one item having the same ID is rendered at one time but I saw some strange problems re-creating the page tree making me belive it's a problem)

I'm using ICEFaces 1.8.2 (but the problem should be implementation independent). What's the solution for this? Using ? Something else? Thanks!

Upvotes: 1

Views: 439

Answers (1)

BalusC
BalusC

Reputation: 1108742

You can put <f:validator> in a JSTL <c:if>. It will be created during tag build time.

<html xmlns:c="http://java.sun.com/jsp/jstl/core">
...
<c:if test="#{type eq email}"><f:validator validatorId="email" /></c:if>

By the way, IceFaces isn't a JSF implementation, it's a JSF component library which is supposed to run on top of a JSF implementation.. Mojarra and MyFaces are JSF implementations.

Upvotes: 1

Related Questions