Thanks Stan,
I will give it a try

Yaron 

-----Original Message-----
From: Stan Carney [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, January 29, 2008 4:18 PM
To: MyFaces Discussion
Subject: Re: jsf validation in diffrent life phases

We do something similar but with messages. On the application side you 
are able to create FacesMessages and associate them with the component 
they reference. The example below is a shortened version of what we use 
with all the non-important bits removed.

public void doSomething(){
FacesContext context = FacesContext.getCurrentInstance();
String componentId = getClientIdById(context.getViewRoot(), context, 
"first-name");
context.addMessage(componentId, new 
FacesMessage(FacesMessage.SEVERITY_ERROR, "An error has occured",
null));
}

public String getClientIdById(UIComponent component, FacesContext 
context, String id) {

if (component.getId() != null && component.getId().equals(id)) {
return component.getClientId(context);
} else {
for (Iterator childIter = component.getChildren().iterator(); 
childIter.hasNext();) {
String matchedComponent = getClientIdById((UIComponent) 
childIter.next(), context, id);
if (matchedComponent != null) {
return matchedComponent;
}
}
return null;
}
}

On the UI we would have a form with an id "details" and an input field 
with an id of "first-name" with an h:message right next to it. 
<h:message class="error" for="first-name"/>. When the application level 
validation fails it would call something like the above code, before 
returning null to redraw the current page, to create an new FacesMessage

object and associate it with "first-name" just like it does when a field

validation fails.

We have been doing this for years and as I recall we found this approach

on the web or in a book. I can't recall which.


Stan


Yaron Spektor wrote:
>
> Hi,
>
> I have a form that I would like to validate. Some of the validations 
> are field validations and some need application-level validation (for 
> example a country must match a state - where State and Country are 2 
> different fields in the form).
>
> I would like to be able to show * next to the erroneous data. This is 
> trivial in field validation, but how do I do it in application level 
> validation?
>
> A workaround I thought of is holding the components in a map and 
> adding an * to them if an error occurred, but I am sure there is a 
> simpler less cumbersome option out there (maybe with phase
listeners?).
>
> I would appreciate any advice,
>
> Thanks,
>
> Yaron,
>

Reply via email to