Reputation: 9658
I've got a custom validator set up to run on the elements of a composite component. It looks like this:
<myproject:myComponent>
<f:validator validatorId="customValidator" for="validatedField" />
</myproject:myComponent>
And the composite component looks like this:
<composite:interface>
<composite:editableValueHolder name="validatedField" targets="validatedField" />
</composite:interface>
<composite:implementation>
<h:inputText id="validatedField" />
</composite:implementation>
It's working, mostly, but there's one hiccup: how do I designate a field as required?
It appears that, when I leave the validated field blank entirely, the custom validator doesn't get called at all. I tried adding a <f:validateRequired for="validatedField" />
inside the mycomponent
, but it didn't work.
<p:outputPanel id="pnlElements">
<ui:repeat value="#{backingViewBean.elementDtos}" var="elementDto">
<p:commandButton value="Edit..."
action="#{backingControllerBean.refreshView}"
ajax="true"
update=":frmMain:editElement"
oncomplete="wdgEditElement.show()">
<f:setPropertyActionListener
target="#higherLevelControllerBean.currentElementId}"
value="#{elementDto.Id}" />
</p:commandButton>
</ui:repeat>
</p:outputPanel>
<p:dialog widgetVar="wdgEditElement" modal="true">
<p:outputPanel id="editElement">
<myproject:myComponent>
<f:validator validatorId="customValidator" for="validatedField" />
</myproject:myComponent>
</p:outputPanel>
</p:dialog>
The page displays a list of the elements I've created, with an edit button (and other stuff I'm omitting) for each. Click the edit button, and a window pops-up containing the composite component (along with a save button, again omitted for clarity) where I can edit the element.
The problem is that, when I set javax.faces.VALIDATE_EMPTY_FIELDS
to true
, the validator gets called before the data object populating the edit window gets set, and it barks at me for having empty fields. (And aborts execution before the data object gets set, so all the fields appear empty.)
(In addition, I notice that when that when VALIDATE_EMPTY_FIELDS
is set to false
, the second and subsequent times I click the Edit button also triggers a call to the validator. This appears harmless, but isn't the behavior I was expecting.)
I'm running under Glassfish, so I'd expect <f:validateRequired>
to be working -- so perhaps I'm using it wrong. I was using it like this:
<myproject:myComponent>
<f:validator validatorId="customValidator" for="validatedField" />
<f:validateRequired for="validatedField" />
</myproject:myComponent>
Am I doing it wrong?
Upvotes: 2
Views: 244
Reputation: 1108632
The <f:validateRequired>
works only if JSR303 bean validation is enabled in your web application (which is usually the case in Java EE web profile compatible servers, such as Glassfish, JBoss AS and TomEE, but thus not in simple servletcontainers like Tomcat). If your webapp doesn't have, then you need to manually force bean validation of empty fields as follows in web.xml
:
<context-param>
<param-name>javax.faces.VALIDATE_EMPTY_FIELDS</param-name>
<param-value>true</param-value>
</context-param>
Upvotes: 3