Visualização combinada Visão plana Exibição em árvore
Tópicos [ Anterior | Próximo ]
toggle
Johann Kneringer
weird behaviour of UIComponent-children in JSF Validator
26 de Junho de 2012 02:09
Resposta

Johann Kneringer

Ranking: Junior Member

Mensagens: 42

Data de entrada: 10 de Novembro de 2011

Mensagens recentes

Hey there.

I have a Validator for required Fields which looks like this:

 1package mypackage;
 2
 3import javax.faces.application.FacesMessage;
 4import javax.faces.component.UIComponent;
 5import javax.faces.component.html.HtmlGraphicImage;
 6import javax.faces.context.FacesContext;
 7import javax.faces.validator.FacesValidator;
 8import javax.faces.validator.Validator;
 9import javax.faces.validator.ValidatorException;
10
11import com.liferay.faces.portal.el.I18N;
12@FacesValidator("validateEmptyFields")
13public class RequiredFieldValidator implements Validator {
14
15    @Override
16    public void validate(FacesContext context, UIComponent component, Object obj) throws ValidatorException {
17       
18        String family = component.getFamily();
19        String componentWarningId = component.getId()+"warning";
20        if(obj==null || obj.toString().equals(""))
21        {
22            //Falls schon ein Warning Symbol vorhanden ist, muss dieses vorher gelöscht werden
23            if(component.getChildren().size()!=0)
24            {   
25                component.getChildren().clear();
26            }
27            
28            if(family.equals("javax.faces.Input"))
29            {
30                component.getAttributes().put("styleClass", "componentErrorStyle");
31            }
32            else if(family.equals("javax.faces.SelectOne"))
33            {
34                component.getAttributes().put("styleClass", "comboboxErrorStyle");
35            }
36            
37            I18N i18n = new I18N();
38            
39            HtmlGraphicImage img = new HtmlGraphicImage();
40            img.setValue("/resources/images/warning.png");
41            img.setId(componentWarningId);
42            img.setHeight("16");
43            img.setTitle(i18n.getString("required"));
44                               
45            UIComponent warningComponent = (UIComponent)img;
46            
47            component.getChildren().add(warningComponent);
48            
49            throw new ValidatorException(new FacesMessage());
50        }
51        else
52        {
53            UIComponent warningComponent =    component.findComponent(componentWarningId);
54            
55            //Wenn Input-Text, dann können alle Kinder gelöscht werden
56            if(family.equals("javax.faces.Input"))
57            {
58                component.getAttributes().put("styleClass", "textBoxes");
59            }
60            //Wenn SelectOne (Menu),dann nicht, da ansonsten die einzelnen SelectItems gelöscht werden
61            else if(family.equals("javax.faces.SelectOne"))
62            {
63                component.getAttributes().put("styleClass", "comboBoxes");
64            }
65            
66            if(warningComponent!=null)
67            {
68                component.getChildren().remove(warningComponent);
69            }
70        }
71    }
72}


The validator is called in Icefaces component using the validator="validateEmptyFields" tag. Some components have partialSubmit="true".

If a field is empty, the styleclass of the component is changed and a warning image is added to the components children...This part works perfect...

The weird behaviour is in the second part, when there is already a warning image of the first validation, and this image has to be removed because there is a value in the component...

The funny thing here is, that the Styleclass-change of the component works perfect, but the image is not getting removed of the children...If I do debug this method, I see, that the image gets deleted from the components children, but on my webpage only the styleclass is getting changed, the image is still being displayed...

I am a little bit confused at this point...Do you have some suggestions?

best regards,
Johann
Neil Griffin
RE: weird behaviour of UIComponent-children in JSF Validator
27 de Junho de 2012 07:57
Resposta

Neil Griffin

LIFERAY STAFF

Ranking: Liferay Legend

Mensagens: 2336

Data de entrada: 26 de Julho de 2005

Mensagens recentes

The typical way of handling this is to simply have the validator throw the exception with a meaningful FacesMessage, and not perform any manipulation of attributes or of the JSF view's component tree.

The JSF API already has a javax.faces.validator.RequiredValidator that does this with FacesMessage that is set with FacesMessage.SEVERITY_ERROR.

The h:message and h:messages component tags have attributes for handing SEVERITY_INFO, SEVERITY_WARNING, SEVERITY_ERROR, and SEVERITY_FATAL. For example:

1<h:message
2  infoClass="portlet-msg-info"
3  warnClass="portlet-msg-warn"
4  errorClass="portlet-msg-error"
5  fatalClass="portlet-msg-error" />


Liferay Faces Bridge will automatically set these for you according to the standard Portlet 2.0 CSS classes. There's nothing for you to do as a JSF portlet developer to take advantage of this.

Additionally, the Liferay Portal CSS will automatically style the h:message or h:messages rendered markup with images and text. Recommend that you look at portal-web/docroot/html/themes/_styled/css/portlet.css in the Liferay Portal source code, or in one of your Liferay Portal themes.
Johann Kneringer
RE: weird behaviour of UIComponent-children in JSF Validator
28 de Junho de 2012 00:25
Resposta

Johann Kneringer

Ranking: Junior Member

Mensagens: 42

Data de entrada: 10 de Novembro de 2011

Mensagens recentes

Hey Neil,

thanks for your reply...

I know that there is a validation implemented for required fields...But i want to make a custom validation, because i want to style the component in a custom way...

If I use the h:message tag, my form gets spread, if there is an error occuring...But I dont want this...

What I want is to update the css style class of the component where the error occurs. I want to give a red border around this component. This is already working...

Additional to this I want to display an image next to the component with the error message in the title, so that the user sees that there is an error and by focusing the image the user will see the error message, because its written in the title...I am able to add an image, but as written above, I'm not able to remove the graphic component, when there is no error in it...


Is there maybe another way to use the required implementation in jsf and just override the output-styling or displaymethod?
Neil Griffin
RE: weird behaviour of UIComponent-children in JSF Validator
28 de Junho de 2012 03:57
Resposta

Neil Griffin

LIFERAY STAFF

Ranking: Liferay Legend

Mensagens: 2336

Data de entrada: 26 de Julho de 2005

Mensagens recentes

I would recommend that you develop a thread-safe PhaseListener that listens to the PROCESS_VALIDATIONS phase of the JSF lifecycle. In the [url=http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/event/PhaseListener.html#afterPhase(javax.faces.event.PhaseEvent)]afterPhase(PhaseEvent) method, you could recurse through the component tree (starting at [url=http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/context/FacesContext.html#getViewRoot()]UIViewRoot) and simply append a CSS class name like "error-highlight" to all instances of EditableValueHolder where the [url=http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/component/UIInput.html#isValid()]isValid() method returns false.

Rather than dynamically adding an image component to the component tree, I think all of the styling should be done by CSS. For example, Liferay's portlet.css displays an image/icon within the error message like this:
1.portlet-msg-error {
2    background-color: #FDD;
3    background-image: url(../images/messages/error.png);
4    border-color: #F00;
5}
Johann Kneringer
RE: weird behaviour of UIComponent-children in JSF Validator
6 de Julho de 2012 03:28
Resposta

Johann Kneringer

Ranking: Junior Member

Mensagens: 42

Data de entrada: 10 de Novembro de 2011

Mensagens recentes

Neil Griffin:
I would recommend that you develop a thread-safe PhaseListener that listens to the PROCESS_VALIDATIONS phase of the JSF lifecycle. In the [url=http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/event/PhaseListener.html#afterPhase(javax.faces.event.PhaseEvent)]afterPhase(PhaseEvent) method, you could recurse through the component tree (starting at [url=http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/context/FacesContext.html#getViewRoot()]UIViewRoot) and simply append a CSS class name like "error-highlight" to all instances of EditableValueHolder where the [url=http://javaserverfaces.java.net/nonav/docs/2.0/javadocs/javax/faces/component/UIInput.html#isValid()]isValid() method returns false.

Rather than dynamically adding an image component to the component tree, I think all of the styling should be done by CSS. For example, Liferay's portlet.css displays an image/icon within the error message like this:
1.portlet-msg-error {
2    background-color: #FDD;
3    background-image: url(../images/messages/error.png);
4    border-color: #F00;
5}



thanks a lot Neil...Got it implemented with your instructions ;-)
Neil Griffin
RE: weird behaviour of UIComponent-children in JSF Validator
6 de Julho de 2012 07:23
Resposta

Neil Griffin

LIFERAY STAFF

Ranking: Liferay Legend

Mensagens: 2336

Data de entrada: 26 de Julho de 2005

Mensagens recentes

That's great news -- so glad to hear that it's working for you. emoticon
Neil Griffin
RE: weird behaviour of UIComponent-children in JSF Validator
25 de Julho de 2012 07:25
Resposta

Neil Griffin

LIFERAY STAFF

Ranking: Liferay Legend

Mensagens: 2336

Data de entrada: 26 de Julho de 2005

Mensagens recentes

News: Looks like the new OmniFaces project has support for highlighting fields that failed validation. I haven't tried it with Liferay Faces but it looks like it has promise.
Johann Kneringer
RE: weird behaviour of UIComponent-children in JSF Validator
2 de Agosto de 2012 00:05
Resposta

Johann Kneringer

Ranking: Junior Member

Mensagens: 42

Data de entrada: 10 de Novembro de 2011

Mensagens recentes

thanks for pointing that out neil. Maybe I'll give that a try in my next project...