You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by Cagatay Civici <ca...@gmail.com> on 2011/05/30 10:12:47 UTC

Clear Input Values

Hi all,

I'd like to discuss something I've been thinking about lately. How to clear forms easily when validation fails?

Consider this simple case;

<h:form>

                <h:messages />
	
                <h:inputText value="#{pprBean.firstname}" required="true"/>
                <h:inputText value="#{pprBean.surname}" required="true"/>

                <h:commandButton value="Save">
                    <f:ajax render="@form" execute="@form"/>
                </h:commandButton>

                <h:commandButton value="Reset" actionListener="#{pprBean.reset}">
                    <f:ajax render="@form" execute="@this"/>
                </h:commandButton>
				
                <h:outputText value="#{pprBean.firstname}" id="display" />
				
</h:form>

Bean;

private String firstname, surname;

    public void reset() {
        firstname = null;
        surname = null;
    }

So when you run this, if one of the field is empty and the other is not, validations fails and message is displayed. Problem happens when reset button is clicked to reset the form values. At processValidations phase UIInput saves the converted value at state
and since validation failed, update model is not executed so local value is never cleared. Clicking reset, clears the bean's values but inputText will not use the bound value and use the one kept in state as well ending up a confusing behavior. I've seen this in many forums.

I know wiki's like this;

http://wiki.apache.org/myfaces/ClearInputComponents

But I mean shouldn't this work as expected? Proposed solutions seem way too hard just to clear form values. (component binding and calling resetValue(), new view, javascript etc.)

If at processValidations phase, local value is not stored in state, I think that will make the code above work, but I'm not sure if there will be any side effects. Does anyone know why converted local value is kept at state by calling setValue().

Cagatay

Re: Clear Input Values

Posted by Jakob Korherr <ja...@gmail.com>.
Hi Cagatay,

This is the exact same issue a customer described to me a year ago.
The result was this blog post [1], but unfortunately not more.

I totally agree with you that this stuff should be easier to use than
it is now! I am describing some ideas to solve this problem in the
blog post (e.g. resetValue() on FacesContext or a
resetOnModelValueUpdate attribute), but maybe there are better ones...

Does primefaces provide something to solve this problem in some sort
of way like trinidad does with the action listener Martin mentioned?

Regards,
Jakob

[1] http://www.jakobk.com/2010/06/validation-errors-and-bean-updates/

2011/5/30 Martin Koci <ma...@gmail.com>:
> Hi,
>
> javax.faces.component.EditableValueHolder.resetValue()  and/or listener
> like
> http://myfaces.apache.org/trinidad/trinidad-api/tagdoc/tr_resetActionListener.html does not solve it?
>
> Kočičák
>
> Mark Struberg píše v Po 30. 05. 2011 v 13:19 +0100:
>> Sounds like a good idea.
>>
>> But shouldn't that be handled via the JSF EG?
>> Such a behaviour would need to be written in the spec to be reliable, isn't?
>>
>> LieGrue,
>> strub
>>
>> --- On Mon, 5/30/11, Cagatay Civici <ca...@gmail.com> wrote:
>>
>> > From: Cagatay Civici <ca...@gmail.com>
>> > Subject: Clear Input Values
>> > To: "MyFaces Development" <de...@myfaces.apache.org>
>> > Date: Monday, May 30, 2011, 8:12 AM
>> > Hi all,
>> >
>> > I'd like to discuss something I've been thinking about
>> > lately. How to clear forms easily when validation fails?
>> >
>> > Consider this simple case;
>> >
>> > <h:form>
>> >
>> >
>> > <h:messages />
>> >
>> >
>> > <h:inputText value="#{pprBean.firstname}"
>> > required="true"/>
>> >
>> > <h:inputText value="#{pprBean.surname}"
>> > required="true"/>
>> >
>> >
>> > <h:commandButton value="Save">
>> >
>> >     <f:ajax render="@form"
>> > execute="@form"/>
>> >
>> > </h:commandButton>
>> >
>> >
>> > <h:commandButton value="Reset"
>> > actionListener="#{pprBean.reset}">
>> >
>> >     <f:ajax render="@form"
>> > execute="@this"/>
>> >
>> > </h:commandButton>
>> >
>> >
>> >
>> > <h:outputText value="#{pprBean.firstname}" id="display"
>> > />
>> >
>> >
>> > </h:form>
>> >
>> > Bean;
>> >
>> > private String firstname, surname;
>> >
>> >     public void reset() {
>> >         firstname = null;
>> >         surname = null;
>> >     }
>> >
>> > So when you run this, if one of the field is empty and the
>> > other is not, validations fails and message is displayed.
>> > Problem happens when reset button is clicked to reset the
>> > form values. At processValidations phase UIInput saves the
>> > converted value at state
>> > and since validation failed, update model is not executed
>> > so local value is never cleared. Clicking reset, clears the
>> > bean's values but inputText will not use the bound value and
>> > use the one kept in state as well ending up a confusing
>> > behavior. I've seen this in many forums.
>> >
>> > I know wiki's like this;
>> >
>> > http://wiki.apache.org/myfaces/ClearInputComponents
>> >
>> > But I mean shouldn't this work as expected? Proposed
>> > solutions seem way too hard just to clear form values.
>> > (component binding and calling resetValue(), new view,
>> > javascript etc.)
>> >
>> > If at processValidations phase, local value is not stored
>> > in state, I think that will make the code above work, but
>> > I'm not sure if there will be any side effects. Does anyone
>> > know why converted local value is kept at state by calling
>> > setValue().
>> >
>> > Cagatay
>>
>
>
>



-- 
Jakob Korherr

blog: http://www.jakobk.com
twitter: http://twitter.com/jakobkorherr
work: http://www.irian.at

Re: Clear Input Values

Posted by Martin Koci <ma...@gmail.com>.
Hi,

javax.faces.component.EditableValueHolder.resetValue()  and/or listener
like
http://myfaces.apache.org/trinidad/trinidad-api/tagdoc/tr_resetActionListener.html does not solve it?

Kočičák

Mark Struberg píše v Po 30. 05. 2011 v 13:19 +0100:
> Sounds like a good idea. 
> 
> But shouldn't that be handled via the JSF EG?
> Such a behaviour would need to be written in the spec to be reliable, isn't?
> 
> LieGrue,
> strub
> 
> --- On Mon, 5/30/11, Cagatay Civici <ca...@gmail.com> wrote:
> 
> > From: Cagatay Civici <ca...@gmail.com>
> > Subject: Clear Input Values
> > To: "MyFaces Development" <de...@myfaces.apache.org>
> > Date: Monday, May 30, 2011, 8:12 AM
> > Hi all,
> > 
> > I'd like to discuss something I've been thinking about
> > lately. How to clear forms easily when validation fails?
> > 
> > Consider this simple case;
> > 
> > <h:form>
> > 
> >                
> > <h:messages />
> >     
> >                
> > <h:inputText value="#{pprBean.firstname}"
> > required="true"/>
> >                
> > <h:inputText value="#{pprBean.surname}"
> > required="true"/>
> > 
> >                
> > <h:commandButton value="Save">
> >                
> >     <f:ajax render="@form"
> > execute="@form"/>
> >                
> > </h:commandButton>
> > 
> >                
> > <h:commandButton value="Reset"
> > actionListener="#{pprBean.reset}">
> >                
> >     <f:ajax render="@form"
> > execute="@this"/>
> >                
> > </h:commandButton>
> >            
> >     
> >                
> > <h:outputText value="#{pprBean.firstname}" id="display"
> > />
> >            
> >     
> > </h:form>
> > 
> > Bean;
> > 
> > private String firstname, surname;
> > 
> >     public void reset() {
> >         firstname = null;
> >         surname = null;
> >     }
> > 
> > So when you run this, if one of the field is empty and the
> > other is not, validations fails and message is displayed.
> > Problem happens when reset button is clicked to reset the
> > form values. At processValidations phase UIInput saves the
> > converted value at state
> > and since validation failed, update model is not executed
> > so local value is never cleared. Clicking reset, clears the
> > bean's values but inputText will not use the bound value and
> > use the one kept in state as well ending up a confusing
> > behavior. I've seen this in many forums.
> > 
> > I know wiki's like this;
> > 
> > http://wiki.apache.org/myfaces/ClearInputComponents
> > 
> > But I mean shouldn't this work as expected? Proposed
> > solutions seem way too hard just to clear form values.
> > (component binding and calling resetValue(), new view,
> > javascript etc.)
> > 
> > If at processValidations phase, local value is not stored
> > in state, I think that will make the code above work, but
> > I'm not sure if there will be any side effects. Does anyone
> > know why converted local value is kept at state by calling
> > setValue().
> > 
> > Cagatay
> 



Re: Clear Input Values

Posted by Cagatay Civici <ca...@gmail.com>.
> But shouldn't that be handled via the JSF EG?
> Such a behaviour would need to be written in the spec to be reliable, isn't?
Yes, I agree, it shouldn't be left to implementations.

> javax.faces.component.EditableValueHolder.resetValue()  and/or listener
> like
> http://myfaces.apache.org/trinidad/trinidad-api/tagdoc/tr_resetActionListener.html does not solve it?
As I've mentioned, sure there are solutions but no easy one.

> I totally agree with you that this stuff should be easier to use than
> it is now! I am describing some ideas to solve this problem in the
> blog post (e.g. resetValue() on FacesContext or a
> resetOnModelValueUpdate attribute), but maybe there are better ones...
> 
> Does primefaces provide something to solve this problem in some sort
> of way like trinidad does with the action listener Martin mentioned?
PrimeFaces doesn't have a solution for this now as I'd like this to be solved in JSF Spec/Impl, however it seems I need to place some workaround for p:input* and p:select* for this.

I'll take this to the EG.

Thanks for all the feedback!

Cagatay

On May 30, 2011, at 3:19 PM, Mark Struberg wrote:

> Sounds like a good idea. 
> 
> But shouldn't that be handled via the JSF EG?
> Such a behaviour would need to be written in the spec to be reliable, isn't?
> 
> LieGrue,
> strub
> 
> --- On Mon, 5/30/11, Cagatay Civici <ca...@gmail.com> wrote:
> 
>> From: Cagatay Civici <ca...@gmail.com>
>> Subject: Clear Input Values
>> To: "MyFaces Development" <de...@myfaces.apache.org>
>> Date: Monday, May 30, 2011, 8:12 AM
>> Hi all,
>> 
>> I'd like to discuss something I've been thinking about
>> lately. How to clear forms easily when validation fails?
>> 
>> Consider this simple case;
>> 
>> <h:form>
>> 
>>                
>> <h:messages />
>>     
>>                
>> <h:inputText value="#{pprBean.firstname}"
>> required="true"/>
>>                
>> <h:inputText value="#{pprBean.surname}"
>> required="true"/>
>> 
>>                
>> <h:commandButton value="Save">
>>                
>>     <f:ajax render="@form"
>> execute="@form"/>
>>                
>> </h:commandButton>
>> 
>>                
>> <h:commandButton value="Reset"
>> actionListener="#{pprBean.reset}">
>>                
>>     <f:ajax render="@form"
>> execute="@this"/>
>>                
>> </h:commandButton>
>>            
>>     
>>                
>> <h:outputText value="#{pprBean.firstname}" id="display"
>> />
>>            
>>     
>> </h:form>
>> 
>> Bean;
>> 
>> private String firstname, surname;
>> 
>>     public void reset() {
>>         firstname = null;
>>         surname = null;
>>     }
>> 
>> So when you run this, if one of the field is empty and the
>> other is not, validations fails and message is displayed.
>> Problem happens when reset button is clicked to reset the
>> form values. At processValidations phase UIInput saves the
>> converted value at state
>> and since validation failed, update model is not executed
>> so local value is never cleared. Clicking reset, clears the
>> bean's values but inputText will not use the bound value and
>> use the one kept in state as well ending up a confusing
>> behavior. I've seen this in many forums.
>> 
>> I know wiki's like this;
>> 
>> http://wiki.apache.org/myfaces/ClearInputComponents
>> 
>> But I mean shouldn't this work as expected? Proposed
>> solutions seem way too hard just to clear form values.
>> (component binding and calling resetValue(), new view,
>> javascript etc.)
>> 
>> If at processValidations phase, local value is not stored
>> in state, I think that will make the code above work, but
>> I'm not sure if there will be any side effects. Does anyone
>> know why converted local value is kept at state by calling
>> setValue().
>> 
>> Cagatay

Çağatay Çivici
Principal Consultant
PrimeFaces Lead | JSF EG Member

Prime Teknoloji
Bilkent Cyberpark, A-303d
06800 Ankara/Turkey
Tel: +90 312 265 05 07
http://www.prime.com.tr


Re: Clear Input Values

Posted by Mark Struberg <st...@yahoo.de>.
Sounds like a good idea. 

But shouldn't that be handled via the JSF EG?
Such a behaviour would need to be written in the spec to be reliable, isn't?

LieGrue,
strub

--- On Mon, 5/30/11, Cagatay Civici <ca...@gmail.com> wrote:

> From: Cagatay Civici <ca...@gmail.com>
> Subject: Clear Input Values
> To: "MyFaces Development" <de...@myfaces.apache.org>
> Date: Monday, May 30, 2011, 8:12 AM
> Hi all,
> 
> I'd like to discuss something I've been thinking about
> lately. How to clear forms easily when validation fails?
> 
> Consider this simple case;
> 
> <h:form>
> 
>                
> <h:messages />
>     
>                
> <h:inputText value="#{pprBean.firstname}"
> required="true"/>
>                
> <h:inputText value="#{pprBean.surname}"
> required="true"/>
> 
>                
> <h:commandButton value="Save">
>                
>     <f:ajax render="@form"
> execute="@form"/>
>                
> </h:commandButton>
> 
>                
> <h:commandButton value="Reset"
> actionListener="#{pprBean.reset}">
>                
>     <f:ajax render="@form"
> execute="@this"/>
>                
> </h:commandButton>
>            
>     
>                
> <h:outputText value="#{pprBean.firstname}" id="display"
> />
>            
>     
> </h:form>
> 
> Bean;
> 
> private String firstname, surname;
> 
>     public void reset() {
>         firstname = null;
>         surname = null;
>     }
> 
> So when you run this, if one of the field is empty and the
> other is not, validations fails and message is displayed.
> Problem happens when reset button is clicked to reset the
> form values. At processValidations phase UIInput saves the
> converted value at state
> and since validation failed, update model is not executed
> so local value is never cleared. Clicking reset, clears the
> bean's values but inputText will not use the bound value and
> use the one kept in state as well ending up a confusing
> behavior. I've seen this in many forums.
> 
> I know wiki's like this;
> 
> http://wiki.apache.org/myfaces/ClearInputComponents
> 
> But I mean shouldn't this work as expected? Proposed
> solutions seem way too hard just to clear form values.
> (component binding and calling resetValue(), new view,
> javascript etc.)
> 
> If at processValidations phase, local value is not stored
> in state, I think that will make the code above work, but
> I'm not sure if there will be any side effects. Does anyone
> know why converted local value is kept at state by calling
> setValue().
> 
> Cagatay