You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by David Delbecq <de...@oma.be> on 2006/11/14 16:50:32 UTC

Custom component, validation messages

Hi,

I have a custom component ( let's say assume has id X ) that is made of
several Input component ( let's say X:a, X:b, X:c)

Is it possible to have the error messages for X also contains the error
for X:a X:b and X:c. I  assume user will want something like
<h:message for="X"/>

and not have to write

<h:message for="X"/>
<h:message for="X:a"/>
<h:message for="X:b"/>
<h:message for="X:c"/>

Is there some mechanics in myfaces that can be of any help to me? Do i
have to manually transfer message in the component, and if yes, when?

Re: Custom component, validation messages

Posted by David Delbecq <de...@oma.be>.
Thanks! This was a very useful response.
David Chandler a écrit :
> Oh, I think I see now. You could write a phase listener to run before
> RENDER_RESPONSE that looks for messages associated with your
> sub-components, removes them from the messages queue, and adds them
> associated with the parent instead, like this... :-)
>
> package com.learnjsf.util.faces;
>
> import java.util.Iterator;
>
> import javax.faces.application.FacesMessage;
> import javax.faces.context.FacesContext;
> import javax.faces.event.PhaseEvent;
> import javax.faces.event.PhaseId;
> import javax.faces.event.PhaseListener;
>
> import org.apache.commons.logging.Log;
> import org.apache.commons.logging.LogFactory;
>
> public class ComponentMessageAggregatorListener implements
> PhaseListener {
>
>     private static final Log log = LogFactory
>             .getLog(ComponentMessageAggregatorListener.class);
>
>     public ComponentMessageAggregatorListener() {
>     }
>
>     public PhaseId getPhaseId() {
>         return PhaseId.RENDER_RESPONSE;
>     }
>
>     public void afterPhase(PhaseEvent event) {
>     }
>
>     public void beforePhase(PhaseEvent event) {
>
>         FacesContext facesContext = event.getFacesContext ();
>         for (Iterator i = facesContext.getClientIdsWithMessages();
> i.hasNext();) {
>             String clientId = (String) i.next();
>             String parentId = null;
>             /*
>              * your parsing logic here to set parent ID if clientId is
> one you
>              * recognize as a subcomponent
>              * if (clientId matches some pattern)
>              *   set parentId = the parent part
>              */
>             // Remove the child component's message(s) and attach to
> the parent instead
>             if (parentId != null)
>                 for (Iterator msgs =
> facesContext.getMessages(clientId); msgs
>                         .hasNext();) {
>                     FacesMessage fm = (FacesMessage) msgs.next ();
>                     facesContext.addMessage(parentId, fm);
>                     msgs.remove();
>                 }
>         }
>     }
>
> }
>
> On 11/15/06, * delbd* <delbd@oma.be <ma...@oma.be>> wrote:
>
>     I was hoping there could be some suggestion on how to transfert
>     messages
>     from component id X:a to component id X. I'd like to give user
>     opportunity to put those error message where ever they want. But
>     maybe i
>     should better do 'component X internal validation that check values of
>     it's subcomponents', it seems more logical now.
>
>     Thanks for your answers, at least i know i am not missing some obvious
>     possibility :)
>
>     Martin Marinschek a écrit :
>     > If he can integrate the message-component in the
>     input-component, then
>     > that's an option, right!
>     >
>     > regards,
>     >
>     > Martin
>     >
>     > On 11/14/06, David Chandler <david.chandler@learnjsf.com
>     <ma...@learnjsf.com>> wrote:
>     >> I may not be understanding your question correctly, but if you're
>     >> adding the
>     >> input sub-components programatically, you could add the associated
>     >> message
>     >> sub-components programatically at the same time. Call setId()
>     on each
>     >> input
>     >> component to give it its own ID that you can then use in for=
>     on the
>     >> corresponding message component.
>     >>
>     >> /dmc
>     >>
>     >> --
>     >> David Chandler
>     >> Development Coach
>     >> learnjsf.com <http://learnjsf.com>
>     >>
>     >> On 11/14/06, David Delbecq <delbd@oma.be <ma...@oma.be>>
>     wrote:
>     >> > Hi,
>     >> >
>     >> > I have a custom component ( let's say assume has id X ) that is
>     >> made of
>     >> > several Input component ( let's say X:a, X:b, X:c)
>     >> >
>     >> > Is it possible to have the error messages for X also contains the
>     >> error
>     >> > for X:a X:b and X:c. I  assume user will want something like
>     >> > <h:message for="X"/>
>     >> >
>     >> > and not have to write
>     >> >
>     >> > <h:message for="X"/>
>     >> > <h:message for="X:a"/>
>     >> > <h:message for="X:b"/>
>     >> > <h:message for="X:c"/>
>     >> >
>     >> > Is there some mechanics in myfaces that can be of any help to
>     me? Do i
>     >> > have to manually transfer message in the component, and if
>     yes, when?
>     >> >
>     >>
>     >>
>     >>
>     >>
>     >
>     >
>
>
>
>
> -- 
> David Chandler
> Development Coach
> learnjsf.com <http://learnjsf.com> 


Re: Custom component, validation messages

Posted by David Chandler <da...@learnjsf.com>.
Oh, I think I see now. You could write a phase listener to run before
RENDER_RESPONSE that looks for messages associated with your sub-components,
removes them from the messages queue, and adds them associated with the
parent instead, like this... :-)

package com.learnjsf.util.faces;

import java.util.Iterator;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ComponentMessageAggregatorListener implements PhaseListener {

    private static final Log log = LogFactory
            .getLog(ComponentMessageAggregatorListener.class);

    public ComponentMessageAggregatorListener() {
    }

    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    public void afterPhase(PhaseEvent event) {
    }

    public void beforePhase(PhaseEvent event) {

        FacesContext facesContext = event.getFacesContext();
        for (Iterator i = facesContext.getClientIdsWithMessages();
i.hasNext();)
{
            String clientId = (String) i.next();
            String parentId = null;
            /*
             * your parsing logic here to set parent ID if clientId is one
you
             * recognize as a subcomponent
             * if (clientId matches some pattern)
             *   set parentId = the parent part
             */
            // Remove the child component's message(s) and attach to the
parent instead
            if (parentId != null)
                for (Iterator msgs = facesContext.getMessages(clientId);
msgs
                        .hasNext();) {
                    FacesMessage fm = (FacesMessage) msgs.next();
                    facesContext.addMessage(parentId, fm);
                    msgs.remove();
                }
        }
    }

}

On 11/15/06, delbd <de...@oma.be> wrote:
>
> I was hoping there could be some suggestion on how to transfert messages
> from component id X:a to component id X. I'd like to give user
> opportunity to put those error message where ever they want. But maybe i
> should better do 'component X internal validation that check values of
> it's subcomponents', it seems more logical now.
>
> Thanks for your answers, at least i know i am not missing some obvious
> possibility :)
>
> Martin Marinschek a écrit :
> > If he can integrate the message-component in the input-component, then
> > that's an option, right!
> >
> > regards,
> >
> > Martin
> >
> > On 11/14/06, David Chandler <da...@learnjsf.com> wrote:
> >> I may not be understanding your question correctly, but if you're
> >> adding the
> >> input sub-components programatically, you could add the associated
> >> message
> >> sub-components programatically at the same time. Call setId() on each
> >> input
> >> component to give it its own ID that you can then use in for= on the
> >> corresponding message component.
> >>
> >> /dmc
> >>
> >> --
> >> David Chandler
> >> Development Coach
> >> learnjsf.com
> >>
> >> On 11/14/06, David Delbecq <de...@oma.be> wrote:
> >> > Hi,
> >> >
> >> > I have a custom component ( let's say assume has id X ) that is
> >> made of
> >> > several Input component ( let's say X:a, X:b, X:c)
> >> >
> >> > Is it possible to have the error messages for X also contains the
> >> error
> >> > for X:a X:b and X:c. I  assume user will want something like
> >> > <h:message for="X"/>
> >> >
> >> > and not have to write
> >> >
> >> > <h:message for="X"/>
> >> > <h:message for="X:a"/>
> >> > <h:message for="X:b"/>
> >> > <h:message for="X:c"/>
> >> >
> >> > Is there some mechanics in myfaces that can be of any help to me? Do
> i
> >> > have to manually transfer message in the component, and if yes, when?
> >> >
> >>
> >>
> >>
> >>
> >
> >
>
>


-- 
David Chandler
Development Coach
learnjsf.com

Re: Custom component, validation messages

Posted by delbd <de...@oma.be>.
I was hoping there could be some suggestion on how to transfert messages
from component id X:a to component id X. I'd like to give user
opportunity to put those error message where ever they want. But maybe i
should better do 'component X internal validation that check values of
it's subcomponents', it seems more logical now.

Thanks for your answers, at least i know i am not missing some obvious
possibility :)

Martin Marinschek a écrit :
> If he can integrate the message-component in the input-component, then
> that's an option, right!
>
> regards,
>
> Martin
>
> On 11/14/06, David Chandler <da...@learnjsf.com> wrote:
>> I may not be understanding your question correctly, but if you're
>> adding the
>> input sub-components programatically, you could add the associated
>> message
>> sub-components programatically at the same time. Call setId() on each
>> input
>> component to give it its own ID that you can then use in for= on the
>> corresponding message component.
>>
>> /dmc
>>
>> -- 
>> David Chandler
>> Development Coach
>> learnjsf.com
>>
>> On 11/14/06, David Delbecq <de...@oma.be> wrote:
>> > Hi,
>> >
>> > I have a custom component ( let's say assume has id X ) that is
>> made of
>> > several Input component ( let's say X:a, X:b, X:c)
>> >
>> > Is it possible to have the error messages for X also contains the
>> error
>> > for X:a X:b and X:c. I  assume user will want something like
>> > <h:message for="X"/>
>> >
>> > and not have to write
>> >
>> > <h:message for="X"/>
>> > <h:message for="X:a"/>
>> > <h:message for="X:b"/>
>> > <h:message for="X:c"/>
>> >
>> > Is there some mechanics in myfaces that can be of any help to me? Do i
>> > have to manually transfer message in the component, and if yes, when?
>> >
>>
>>
>>
>>
>
>


Re: Custom component, validation messages

Posted by Martin Marinschek <ma...@gmail.com>.
If he can integrate the message-component in the input-component, then
that's an option, right!

regards,

Martin

On 11/14/06, David Chandler <da...@learnjsf.com> wrote:
> I may not be understanding your question correctly, but if you're adding the
> input sub-components programatically, you could add the associated message
> sub-components programatically at the same time. Call setId() on each input
> component to give it its own ID that you can then use in for= on the
> corresponding message component.
>
> /dmc
>
> --
> David Chandler
> Development Coach
> learnjsf.com
>
> On 11/14/06, David Delbecq <de...@oma.be> wrote:
> > Hi,
> >
> > I have a custom component ( let's say assume has id X ) that is made of
> > several Input component ( let's say X:a, X:b, X:c)
> >
> > Is it possible to have the error messages for X also contains the error
> > for X:a X:b and X:c. I  assume user will want something like
> > <h:message for="X"/>
> >
> > and not have to write
> >
> > <h:message for="X"/>
> > <h:message for="X:a"/>
> > <h:message for="X:b"/>
> > <h:message for="X:c"/>
> >
> > Is there some mechanics in myfaces that can be of any help to me? Do i
> > have to manually transfer message in the component, and if yes, when?
> >
>
>
>
>


-- 

http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces

Re: Custom component, validation messages

Posted by David Chandler <da...@learnjsf.com>.
I may not be understanding your question correctly, but if you're adding the
input sub-components programatically, you could add the associated message
sub-components programatically at the same time. Call setId() on each input
component to give it its own ID that you can then use in for= on the
corresponding message component.

/dmc

-- 
David Chandler
Development Coach
learnjsf.com

On 11/14/06, David Delbecq <de...@oma.be> wrote:
>
> Hi,
>
> I have a custom component ( let's say assume has id X ) that is made of
> several Input component ( let's say X:a, X:b, X:c)
>
> Is it possible to have the error messages for X also contains the error
> for X:a X:b and X:c. I  assume user will want something like
> <h:message for="X"/>
>
> and not have to write
>
> <h:message for="X"/>
> <h:message for="X:a"/>
> <h:message for="X:b"/>
> <h:message for="X:c"/>
>
> Is there some mechanics in myfaces that can be of any help to me? Do i
> have to manually transfer message in the component, and if yes, when?
>

Re: Custom component, validation messages

Posted by Martin Marinschek <ma...@gmail.com>.
No, there's nothing in MyFaces which could handle this AFAIK. I
suppose you could write a new component (or changing t:message) which
also collects the messages of all children of the comp by visiting the
sub-tree under the parent-component (Attention: this doesn't work with
UIData).

regards,

Martin

On 11/14/06, David Delbecq <de...@oma.be> wrote:
> Hi,
>
> I have a custom component ( let's say assume has id X ) that is made of
> several Input component ( let's say X:a, X:b, X:c)
>
> Is it possible to have the error messages for X also contains the error
> for X:a X:b and X:c. I  assume user will want something like
> <h:message for="X"/>
>
> and not have to write
>
> <h:message for="X"/>
> <h:message for="X:a"/>
> <h:message for="X:b"/>
> <h:message for="X:c"/>
>
> Is there some mechanics in myfaces that can be of any help to me? Do i
> have to manually transfer message in the component, and if yes, when?
>


-- 

http://www.irian.at

Your JSF powerhouse -
JSF Consulting, Development and
Courses in English and German

Professional Support for Apache MyFaces