13

I need to verify whether an optional attribute has been passed or not within my composite component. How can I achieve this?

<composite:interface>
    <composite:attribute name="attr1" />
    <composite:attribute name="attr2" required="false" /> <!-- means OPTIONAL -->
</composite:interface>
<composite:implementation>
    <!-- How I can verify here whether attr2 is present or not whenever this component is used? -->
</composite:implementation>

Setting the default attribute to xxx for <composite:attribute> is not what I'm looking for.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
MyFist
  • 413
  • 7
  • 19

3 Answers3

13

You could just check if #{not empty cc.attrs.attr2} evaluates to true.

E.g. inside the rendered attribute of an arbitrary component:

<composite:implementation>
    <h:panelGroup rendered="#{not empty cc.attrs.attr2}">
        Attribute attr2 is not empty!
    </h:panelGroup>
</composite:implementation>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • by the way it is checking the value of `attr2` is empty or not and rendering accordingly. – MyFist Dec 05 '12 at 06:46
  • It is not working as I expected. It is checking the VALUE of `attr2` is empty (null) or not and rendering accordingly. But I want to add a check (with in my CC implementation) if `attr2` itself is PRESENT or not. Say my check on `` should give me `true` as `attr2` itself is passed. AND `` should give me `false` as `attr2` itself is not passed. Please suggest. – MyFist Dec 05 '12 at 06:59
  • You could compare to `null`. `rendered="#{cc.attrs.attr2 != null}"`. – BalusC Dec 05 '12 at 10:45
  • What's the functional requirement for this strange approach? – BalusC Dec 05 '12 at 11:58
  • Sorry for late reply. There is no functional requirement but I want my CC component simple to look. – MyFist Dec 10 '12 at 17:54
4

You can check to see if the expression exists using the method:

cc.getValueExpression('someAttribute')

<composite:implementation>
    <h:outputText rendered="#{cc.getValueExpression('attr2') ne null}">
        Attribute attr2 has been passed!
    </h:outputText>
</composite:implementation>
twinj
  • 2,009
  • 18
  • 10
1

You can conditionally add attributes to component via:

<c:if><f:attribute>

Sample:

<composite:interface>
    <composite:attribute name="label" />
    <composite:attribute name="required" default="false" />
    <composite:attribute name="readonly" default="false" />
    <composite:attribute name="value" />
    <composite:attribute name="title" />
    <composite:attribute name="placeholder" />
    <composite:attribute name="maxlength" type="java.lang.Integer"/>
</composite:interface>
<composite:implementation>
    <p:inputText
      id="field"
      value="#{cc.attrs.value}">
        <c:if test="#{empty cc.attrs.maxLength}">
           <f:attribute name="maxlength" value="#{cc.attrs.maxlength}" />
        </c:if>
    </p:inputText>
</composite:implementation>

I Found the answer in:

How not to set an attribute of a component inside a composite component if it is empty?

zoki
  • 19
  • 3