Validating a JSF Composite Component

I want to create a composite component and attach some validators to it, but I want the verification message to be attached to the composite component, and not to it.

On a page using a composite component, I want something like this:

<zzz:mycomponent id="my" /> <h:message for="my" /> 

Now this does not work, because the message is intended for a child component, and not for a composite component. How to do this for the whole component?

Or even better, I would like to add a validator to the composite component, for example:

 <zzz:mycomponent id="my" validator="#{bean.validateComposite}" /> 

And get something like a booleans array as a value, because there are h:selectBooleanCheckbox elements inside the composite component. Is it possible?

+4
source share
2 answers

It may be very late to answer this question, but here is how I should do it:

 <zzz:mycomponent id="my"> <f:event type="postValidate" listener="#{bean.doValidation}"/> </zzz:mycomponent> 

Then doValidation is verified after checking the children in the "container". The method is as follows:

 public void doValidation(ComponentSystemEvent event) { ... } 

And you have 2 options in this method:

  • Access the child components (event.getComponent (). GetChildren ()) and do whatever you want with the values ​​represented on these child elements.

  • Or loop into FacesMessages and redistribute clientId so that they are placed in the component of your container (id = my)

+1
source

You must put the following code in your composite definition.

  <cc:interface> ..... <cc:editableValueHolder name="attName" targets="Idcomponent" /><!--It allows to acces to the composite--> <cc:facet name="textMessage"/> <!--Define the Facet--> </cc:interface> <cc:implementation id="#{cc.attrs.id}" > ...... <h:inputText id="Idcomponent" value="#{cc.attrs.value}" required="#{cc.attrs.required}"/> <cc:renderFacet name="textMessage"/> </cc:implementation> 

You can use the JSF page

  <zzz:textBox id="txbTest" label="#{}" value="#{}" > <f:validateLongRange for="attName" minimum="-10" maximum="10"/> <f:facet name="textMessage"> <h:message for="value" style="color: blue"/> </f:facet> </zzz:textBox> 

Or there may be another option:

  <zzz:textBox id="txbTest" label="#{}" value="#{}" validator="#{bean.yourValidateMethod}" > <f:facet name="textMessage"> <h:message for="value" style="color: blue"/> </f:facet> </zzz:textBox> 
+1
source

All Articles