Use 'Message' Tags For Specific Tables Or Elements with Richfaces
Although the
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
public class TestValidator implements Validator {
@Override
public void validate(FacesContext arg0, UIComponent arg1, Object arg2) {// throws ValidatorException {
int myVal = (Integer)arg2;
Object ret = arg1.getAttributes().get("target");
if(myVal < 0) {
FacesMessage myMsg = new FacesMessage();
myMsg.setSeverity(FacesMessage.SEVERITY_WARN);
myMsg.setDetail("Detail comin' here");
myMsg.setSummary("Summary goes here");
if(ret != null && ret instanceof String) {
String target = (String)ret;
javax.faces.context.FacesContext.getCurrentInstance().addMessage(target, myMsg);
}
}
}
}
As you might have seen, I also passed the value of the <[rich h]:message[s]> tag’s for attribute (used as Object ret and String target in the code above). To complete the configuation of the custom validator we need to decalre it in the faces-config.xml:
...
<validator>
<validator-id>test.TestValidator</validator-id>
<validator-class>my.package.TestValidator</validator-class>
</validator>
...
The usage of this validator is shown in the following example:
...
<h:outputText value="Table1"/>
<rich:messages for="testForm:myMsgField"/>
<rich:dataTable id="tab1" value="#{remoteTestClass.tableOne}"....>
....
<h:inputText id="myMsgField" value="#{remoteTestClass.testField}">
<a4j:support event="onchange" ajaxSingle="true" action="#{remoteTestClass.test2()}"/>
<f:validator validatorId="test.TestValidator"/>
<f:attribute name="target" value="testForm:myMsgField"/>
</h:inputText>
...
AFAIK, it is not possible to pass the an argument as part of the ** tag, so I passed it as part of the parent ** tag. What is more, the usage of the message tag with the for atrtibute needs a corresponding input field to work as expected, meaning to display faked of that field. 'Faked' because we don't use exactly this input field but the input field inside the table:
...
<h:inputText id="myMsgField" rendered="false"/>
...