JSF 2.0: h:inputText inside composite component fails with non-String objects when validation is set
Asked Answered
C

1

2

In a backing bean:

@Min(3)
Integer foo;

If I have form like:

<h:form>
    <h:commandButton value="Submit" />
    <h:inputText value="#{bean.foo}" />
</h:form>

This works ok. However, if I do something like

<cc:interface>
    <cc:attribute name="text" />
    <cc:editableValueHolder name="text" targets="field" />
<cc:interface>
<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}" />
</cc:implementation>

and call this inside form instead of directly h:inputText as in:

<!-- <h:inputText value="#{bean.foo}" /> -->
<pref:fieldComponent text="#{bean.foo}" />

But then I get:

javax.validation.ValidationException: Unexpected exception during isValid call
    at org.hibernate.validator.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:144)
    at org.hibernate.validator.engine.ConstraintTree.validateConstraints(ConstraintTree.java:118)
    at org.hibernate.validator.metadata.MetaConstraint.validateConstraint(MetaConstraint.java:121)
    at org.hibernate.validator.engine.ValidatorImpl.validateValueForGroup(ValidatorImpl.java:655)
    ...

And the root cause is:

Caused by: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Number
    at org.hibernate.validator.constraints.impl.MinValidatorForNumber.isValid(MinValidatorForNumber.java:32)
    at org.hibernate.validator.engine.ConstraintTree.validateSingleConstraint(ConstraintTree.java:141)
    ... 69 more

If I remove validation, it works. Also, if foo is of type String, it works also with validations.

I tried playing with cc:editableValueHolder, defining different types (also omitting it) and a few other tricks but I am a bit unsure how to actually implement this. Or is it a bug? Seems like it's forgetting to use a converter? Have I misunderstood something?

Centuplicate answered 9/2, 2011 at 10:19 Comment(3)
You've always the weirdest problems. Haven't seen this one before as well. Which JSF impl/version? Which JSR303 impl/version? What is the 1st stacktrace line of the both exceptions?Toxophilite
Fortunately the kind folks of SO are always there to help :) Still running Mojarra 2.0.2 but I also tried this on the newest 2.0.4 and 2.1.0 betas. I'll add few stacktrace lines to the question...Centuplicate
I opened up a ticket for this in Mojarra tracker: java.net/jira/browse/JAVASERVERFACES-1953Centuplicate
T
1

As per a comment on your ticket, it turns out that you could as workaround explicitly specify the type converter.

You could do it as follows

<pref:fieldComponent text="#{bean.foo}">
    <f:converter converterId="javax.faces.Integer" />
</pref:fieldComponent>

and

<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}">
        <cc:insertChildren />
    </h:inputText>
</cc:implementation>

or maybe

<pref:fieldComponent text="#{bean.foo}" converter="javax.faces.Integer" />

and

<cc:implementation>
    <h:inputText id="field" value="#{cc.attrs.text}" converter="#{cc.attrs.converter}" />
</cc:implementation>
Toxophilite answered 12/8, 2011 at 17:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.