Error handling

Hello,

I have a question concerning the error handling. I have a site with input fields. Furthermore I want to show all validation errors at the top of the site. This is not the problem, but additionally to the messages at the top I want to mark the input fields, e.g. with an asterix, which are invalid. I tried a lot but I don't know how to identifiy the error input field. For example

<h:form id="formId">

<h:outputText style="color:red" value="${my:errorAvailable('formId:lastname_id')}">Last name</h:outputText>

<h:inputText id="lastname" value="${person.lastName}" validator="${person.checkLastname}" required="true" />

</h:form>

The static function 'errorAvailable' should check if there exists an error message regarding to the lastname input field. Thereto I iterate over the error messages stored in the faces context and compare the ids. If an error has been found the method returns the asterix string. If no error has been found the empty string will be returned.

The problem is, that the function 'errorAvailable' has to know the exact id to verify if the facesContext contains an error message with this id this means not only 'lastname_id' but 'formId:lastName_id'. In the example above I specified this id but if the structure of the site changes, the id is invalid. How can I get dynamic access to this id or how can I handle this in another way? Please give me some inspiration ...

Best regards,

Ulrich Frank

[1531 byte] By [frankulricha] at [2007-11-26 16:07:41]
# 1

If I understand correctly, the solution should be resolved by doing the following in your validator method and/or classes.

If the validation fails,

public boolean validateGroupName(FacesContext context,

UIComponent toValidate,

Object value) {

String stringValue = (String) value;

.... some code logic here ...

IF (it fails, then write the following in the validate method) {

String msg = new String("It failed");

((UIInput)toValidate).setValid(false);

FacesMessage message = new FacesMessage(msg);

context.addMessage(toValidate.getClientId(context), message);

etcetera

Then in your code, place an h:message tag

<h:message for="lastname"/>

Faces will take care of whether or not this error is displayed.

wbossonsa at 2007-7-8 22:29:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Could you do something like:

<h:outputText rendered="#{not empty my:errorAvailable('formId:lastname_id')}">*</h:outputText>

The rendered attribute is sort of like a conditional statement for JSF.

Remember that JSF EL uses #{ ... } and not ${ ... } (which is JSTL).

anthony.c.smith@insightbb.co at 2007-7-8 22:29:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
This way it doesn't work in EL.wbossons is right. Use h:message. And in the validator you indeed just can get the clientId by UIComponent.getClientId().
BalusCa at 2007-7-8 22:29:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
Agreed -- if there is no message for the component/element, then the h:message will not be rendered -- no need to add any rendered attribute and supporting code.
wbossonsa at 2007-7-8 22:29:59 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...