myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Andrew Robinson" <andrew.rw.robin...@gmail.com>
Subject Re: Detecting whether or not the user changed anything on a form
Date Fri, 13 Jul 2007 15:48:42 GMT
You don't need a phase listener. You could use one bean if you want.
Here is just one example that you could use to track changes:

public class ChangeTrackingListener implements ValueChangeListener {
  private final static String CHANGED_KEY = "ChangeTrackingListener";
  public void processValueChange(ValueChangeEvent event) {
    FacesContext fctx = FacesContext.getCurrentInstance();
    fctx.getExternalContext().getRequestMap().put(CHANGED_KEY, Boolean.TRUE);
  }
  public static boolean hasChanges() {
    FacesContext fctx = FacesContext.getCurrentInstance();
    Boolean value = (Boolean)fctx.getExternalContext()
      .getRequestMap().get(CHANGED_KEY);
    return (value != null && value.booleanValue());
  }
}

<h:inputText id="..." value="#{...}>
  <f:valueChangeListener type="example.ChangeTrackingListener" />
</h:inputText>

Now this is a VERY simple example. You could change it to determine
what changed, instead of just if there just was a change, etc.

-Andrew

On 7/13/07, Hannum, Daniel <Daniel_Hannum@premierinc.com> wrote:
> Thanks. Pardon my inexperience as I think this through
>
> So I could write a phase listener that would check for queued ValueChangeEvents during
the validation phase and the listener would set some flag on one backing bean. The flag would
indicate "Did the last JSF form submission that occurred contain any changes?" I also have
to be careful of the scoping of that bean since it's shared across all pages. I think session
would work.
>
> Does that design make sense? I'm new to PhaseListeners and the right way to use them.
>
> Dan
>
>
>  -----Original Message-----
> From: Andrew Robinson [mailto:andrew.rw.robinson@gmail.com]
> Sent: Friday, July 13, 2007 10:52 AM
> To: MyFaces Discussion
> Subject: Re: Detecting whether or not the user changed anything on a form
>
> If at least one ValueChangeEvent is queued (by a UIInput component)
> during the validation pahse, then something has changed.
>
> On 7/13/07, Hannum, Daniel <Daniel_Hannum@premierinc.com> wrote:
> >
> >
> >
> >
> > Hi,
> >
> >
> >
> > I have a form for editing a business entity. The form is prepopulated with
> > the current values and the user can change everything and hit the submit
> > button when their done. Fairly simple. What's tricky is that I'd like to
> > only show a confirmation ("Changes saved") message if they actually changed
> > something instead of just submitting the same values as before.
> >
> >
> >
> > The naïve way is to check each backing bean field against the field in the
> > business entity, but that's unmaintainable when the object starts changing.
> > Is there a framework-based way to detect this? I want to avoid having to
> > enumerate every field when I check. I want to know that _at least one_ of
> > the fields on the page has a submitted value different from the model value.
> > (I think I got the terminology right)
> >
> >
> >
> > Maybe the answer lies in a phase listener that would somehow do that check,
> > my experience with that is limited. Please let me know if this is possible
> > or not.
> >
> >
> >
> > And I'm using MyFaces and Trinidad so hopefully it would only use mechanisms
> > include in those projects.
> >
> >
> >
> > Thanks
> >
> > Dan
> >
> >
> > ________________________________
> >
> >
> >
> >
> >  ***Note:The information contained in this message may be privileged and
> > confidential and protected from disclosure. If the reader of this message is
> > not the intended recipient, or an employee or agent responsible for
> > delivering this message to the intended recipient, you are hereby notified
> > that any dissemination, distribution or copying of this communication is
> > strictly prohibited. If you have received this communication in error,
> > please notify the Sender immediately by replying to the message and deleting
> > it from your computer. Thank you. Premier Inc.
>
> -----------------------------------------
> ***Note:The information contained in this message may be privileged
> and confidential and protected from disclosure. If the reader of
> this message is not the intended recipient, or an employee or agent
> responsible for delivering this message to the intended recipient,
> you are hereby notified that any dissemination, distribution or
> copying of this communication is strictly prohibited. If you have
> received this communication in error, please notify the Sender
> immediately by replying to the message and deleting it from your
> computer. Thank you. Premier Inc.
>

Mime
View raw message