You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by HarryBakker <ha...@gmail.com> on 2011/06/25 23:53:34 UTC

How can i reset the data in TextFields?

I'm a relative newby with Wicket and am struggling to get something to work.
Maybe somebody can help me?

I have a form with some TextFields on it, that i'm using to keep track of
some properties of a User. Some fields are required (they have the
required() attribute set to true), some not. Now i want to have a 'reset'
button on the form that let's me reset all fields to their original value. I
have the setDefaultFormProcessing() property of this resetbutton set to
false, to prevent validation of the required fields that aren't currently
filled by the user. In the onSubmit() of the button, i do the following for
every button (i'm only showing the code for this single button; the rest are
coded along the same lines):

Form form = (Form) get("form);
TextField name = (TextField) form.get("name);
name.setDefaultModelObject(originalUser.getName());
// ... rest of the fields ...

After all fields are set to their original values the page is refreshed, but
the TextFields contents retain their current value. They are not reset to
their original value, which i expected.

What am i doing wrong?

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3625099.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How can i reset the data in TextFields?

Posted by Igor Vaynberg <ig...@gmail.com>.
Use a link and call form.clearinput()

-igor
On Jun 25, 2011 4:23 PM, "HarryBakker" <ha...@gmail.com> wrote:
> I'm a relative newby with Wicket and am struggling to get something to
work.
> Maybe somebody can help me?
>
> I have a form with some TextFields on it, that i'm using to keep track of
> some properties of a User. Some fields are required (they have the
> required() attribute set to true), some not. Now i want to have a 'reset'
> button on the form that let's me reset all fields to their original value.
I
> have the setDefaultFormProcessing() property of this resetbutton set to
> false, to prevent validation of the required fields that aren't currently
> filled by the user. In the onSubmit() of the button, i do the following
for
> every button (i'm only showing the code for this single button; the rest
are
> coded along the same lines):
>
> Form form = (Form) get("form);
> TextField name = (TextField) form.get("name);
> name.setDefaultModelObject(originalUser.getName());
> // ... rest of the fields ...
>
> After all fields are set to their original values the page is refreshed,
but
> the TextFields contents retain their current value. They are not reset to
> their original value, which i expected.
>
> What am i doing wrong?
>
> --
> View this message in context:
http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3625099.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

Re: How can i reset the data in TextFields?

Posted by Igor Vaynberg <ig...@gmail.com>.
On Mon, Jun 27, 2011 at 12:54 AM, HarryBakker <ha...@gmail.com> wrote:
> Hi Igor,
>
> I think i get the idea.
>
> To summarise in my own brief way: Every TextField (among others) has besides
> a Model, also an input buffer. This buffer contains what the user typed. It
> is also the contents of this buffer that is subject to validation. Only when
> validation succeeds, the buffers contents is stored in the Model (after a
> successful conversion). It is also the contents of this buffer that is being
> displayed on the screen. Therefore, a copy is made from the Model value to
> the buffer before display. Can you confirn this summary?

close enough.

-igor

>
> So, in my application, when i press the Cancel button, i want to get rid of
> the user input by clearing the buffer, which can be done by a call to
> clearInput(). This is probably implemented in Wicket by doing a copy from
> the Model to the buffer.
>
> I have implemented it and it works fine!
>
> Thanks for your advice.
>
> Cheers,
>
> -Harry
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3627166.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How can i reset the data in TextFields?

Posted by HarryBakker <ha...@gmail.com>.
Hi Igor,

I think i get the idea.

To summarise in my own brief way: Every TextField (among others) has besides
a Model, also an input buffer. This buffer contains what the user typed. It
is also the contents of this buffer that is subject to validation. Only when
validation succeeds, the buffers contents is stored in the Model (after a
successful conversion). It is also the contents of this buffer that is being
displayed on the screen. Therefore, a copy is made from the Model value to
the buffer before display. Can you confirn this summary?

So, in my application, when i press the Cancel button, i want to get rid of
the user input by clearing the buffer, which can be done by a call to
clearInput(). This is probably implemented in Wicket by doing a copy from
the Model to the buffer.

I have implemented it and it works fine!

Thanks for your advice.

Cheers,

-Harry

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3627166.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: How can i reset the data in TextFields?

Posted by Igor Vaynberg <ig...@gmail.com>.
You can user a button with default form processing set to false, but you
still have to call form.clearinput() so that form components pull their
values from the model instead of using the values with which the form was
submitted. However, since you are dismissing the submitted values anyways it
makes sense to use a link.

-igor
On Jun 26, 2011 6:13 AM, "HarryBakker" <ha...@gmail.com> wrote:
> Thanks Igor for your solution.
>
> However, cannot figure out /why/ i can't use a button.
> Let me explain a little about the mechanics of the page (code is at the
> bottom). I can use it to edit data of an existing User (pagestate=EDIT) or
> create a new one (pagestate=NEW). On entry of the page i check for the
> existence of a userid(='code') in de pageparameters and if it is there the
> *originalGebruiker* object is filled with its corresponding data. If it
> isn't there, the *originalGebruiker* object is filled with blank strings.
> The components on the page are binded to the propterties of the
*gebruiker*
> object. Now when i press the Cancel button, the data that is already
entered
> by the user must be reset to their original value, regardless of the
> pagestate. This can easily be done by copying the *originalGebruiker*
object
> to the *gebruiker* object and then refresh the page. When i switch to NEW
> mode by pressing the newButton, i fill the *originalGebruiker* object with
> blank strings and also do a copy from *originalGebruiker* to *gebruiker*
and
> refresh. This copy however is what fails (see my previous email). The data
> the user entered is NOT erased by the *originalGebruiker* data.Nothing
> happens.
>
> Here's the code:
>
> public class OnderhoudGebruikerNewPage extends BPRBasePage {
>
> @SpringBean
> private GebruikerService gebruikerService;
> private enum PageState { EDIT, NEW }
> private AutorisatieGebruiker gebruiker = new AutorisatieGebruiker();
> private AutorisatieGebruiker originalGebruiker = new
> AutorisatieGebruiker();
> private String password1;
> private String password2;
> private PageState state = PageState.NEW;
>
> /**
> * Constructor
> * @param params
> */
> public OnderhoudGebruikerNewPage(PageParameters params) {
> createComponents();
> originalGebruiker = null;
> if (params.containsKey("code")) {
> originalGebruiker =
> gebruikerService.find(params.getString("code"));
> }
> if (originalGebruiker == null) {
> originalGebruiker = new AutorisatieGebruiker("", "", "", "");
> state = PageState.NEW;
> } else {
> state = PageState.EDIT;
> }
> resetData();
> }
>
> private void createComponents() {
> Form form = new Form("form");
> add(form);
> // Add components to the form.
> form.add(new TextField("codeField", new PropertyModel(gebruiker,
> "code")).setRequired(true));
> form.add(new TextField("nameField", new PropertyModel(gebruiker,
> "naam")).setRequired(true));
> form.add(new TextField("organisationField", new
> PropertyModel(gebruiker, "organisatie")));
> form.add(new PasswordTextField("password1Field", new
> PropertyModel(this, "password1")));
> form.add(new PasswordTextField("password2Field", new
> PropertyModel(this, "password2")));
> form.add(new Button("cancelButton") {
> @Override
> public void onSubmit() {
> processCancel();
> }
> }.setDefaultFormProcessing(false));
> form.add(new Button("saveButton") {
> @Override
> public void onSubmit() {
> processSave();
> }
> });
> form.add(new Button("newButton") {
> @Override
> public void onSubmit() {
> processNew();
> }
> });
> form.add(new Button("deleteButton") {
> @Override
> public void onSubmit() {
> processDelete();
> }
> }.setDefaultFormProcessing(false));
> form.add(new Button("searchButton") {
> @Override
> public void onSubmit() {
> processSearch();
> }
> }.setDefaultFormProcessing(false));
> FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
> add(feedbackPanel);
> }
>
> /**
> * Fill fields with data.
> */
> private void resetData() {
> // Get references to all fields.
> Form form = (Form) get("form");
> TextField codeField = (TextField) form.get("codeField");
> TextField nameField = (TextField) form.get("nameField");
> TextField organisationField = (TextField)
> form.get("organisationField");
> PasswordTextField passwordField1 = (PasswordTextField)
> form.get("password1Field");
> PasswordTextField passwordField2 = (PasswordTextField)
> form.get("password2Field");
>
> // Fill them with appropriate data.
> codeField.setDefaultModelObject(originalGebruiker.getCode());
> nameField.setDefaultModelObject(originalGebruiker.getNaam());
>
>
organisationField.setDefaultModelObject(originalGebruiker.getOrganisatie());
>
> passwordField1.setDefaultModelObject(originalGebruiker.getWachtwoord());
> passwordField2.setDefaultModelObject("");
> }
>
> private void changeState(PageState newState) {
> state = newState;
> // Now change state of some fields based on page state.
> Form form = (Form) get("form");
> TextField codeField = (TextField) form.get("codeField");
> if (state == PageState.EDIT) {
> codeField.setEnabled(false);
> } else {
> codeField.setEnabled(true);
> }
> }
>
> private void processSearch() {
> // Goto the search page.
> setRedirect(false);
> setResponsePage(OnderhoudGebruikerSearchPage.class);
> }
>
> private void processDelete() {
> // TODO
> }
>
> private void processNew() {
> originalGebruiker = new AutorisatieGebruiker("", "", "", "");
> changeState(PageState.NEW);
> resetData();
> }
>
> private void processSave() {
> // TODO
> }
>
> private void processCancel() {
> resetData();
> }
> }
>
> It would help if you could give me some insight about the reason i cannot
> use a button and why the principle i use fails.
>
> Thanks in advance.
>
> --
> View this message in context:
http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3625841.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>

Re: How can i reset the data in TextFields?

Posted by HarryBakker <ha...@gmail.com>.
Thanks Igor for your solution.

However, cannot figure out /why/ i can't use a button. 
Let me explain a little about the mechanics of the page (code is at the
bottom). I can use it to edit data of an existing User (pagestate=EDIT) or
create a new one (pagestate=NEW). On entry of the page i check for the
existence of a userid(='code') in de pageparameters and if it is there the
*originalGebruiker* object is filled with its corresponding data. If it
isn't there, the *originalGebruiker* object is filled with blank strings.
The components on the page are binded to the propterties of the *gebruiker*
object. Now when i press the Cancel button, the data that is already entered
by the user must be reset to their original value, regardless of the
pagestate. This can easily be done by copying the *originalGebruiker* object
to the *gebruiker* object and then refresh the page. When i switch to NEW
mode by pressing the newButton, i fill the *originalGebruiker* object with
blank strings and also do a copy from *originalGebruiker* to *gebruiker* and
refresh. This copy however is what fails (see my previous email). The data
the user entered is NOT erased by the *originalGebruiker* data.Nothing
happens.

Here's the code:

public class OnderhoudGebruikerNewPage extends BPRBasePage {

    @SpringBean
    private GebruikerService gebruikerService;
    private enum PageState { EDIT, NEW }
    private AutorisatieGebruiker gebruiker = new AutorisatieGebruiker();
    private AutorisatieGebruiker originalGebruiker = new
AutorisatieGebruiker();
    private String password1;
    private String password2;
    private PageState state = PageState.NEW;

    /**
     * Constructor
     * @param params
     */
    public OnderhoudGebruikerNewPage(PageParameters params) {
        createComponents();
        originalGebruiker = null;
        if (params.containsKey("code")) {
            originalGebruiker =
gebruikerService.find(params.getString("code"));
        }
        if (originalGebruiker == null) {
            originalGebruiker = new AutorisatieGebruiker("", "", "", "");
            state = PageState.NEW;
        } else {
            state = PageState.EDIT;
        }
        resetData();
    }

    private void createComponents() {
        Form form = new Form("form");
        add(form);
        // Add components to the form.
        form.add(new TextField("codeField", new PropertyModel(gebruiker,
"code")).setRequired(true));
        form.add(new TextField("nameField", new PropertyModel(gebruiker,
"naam")).setRequired(true));
        form.add(new TextField("organisationField", new
PropertyModel(gebruiker, "organisatie")));
        form.add(new PasswordTextField("password1Field", new
PropertyModel(this, "password1")));
        form.add(new PasswordTextField("password2Field", new
PropertyModel(this, "password2")));
        form.add(new Button("cancelButton") {
            @Override
            public void onSubmit() {
                processCancel();
            }
        }.setDefaultFormProcessing(false));
        form.add(new Button("saveButton") {
            @Override
            public void onSubmit() {
                processSave();
            }
        });
        form.add(new Button("newButton") {
            @Override
            public void onSubmit() {
                processNew();
            }
        });
        form.add(new Button("deleteButton") {
            @Override
            public void onSubmit() {
                processDelete();
            }
        }.setDefaultFormProcessing(false));
        form.add(new Button("searchButton") {
            @Override
            public void onSubmit() {
                processSearch();
            }
        }.setDefaultFormProcessing(false));
        FeedbackPanel feedbackPanel = new FeedbackPanel("feedback");
        add(feedbackPanel);
    }

    /**
     * Fill fields with data.
     */
    private void resetData() {
        // Get references to all fields.
        Form form = (Form) get("form");
        TextField codeField = (TextField) form.get("codeField");
        TextField nameField = (TextField) form.get("nameField");
        TextField organisationField = (TextField)
form.get("organisationField");
        PasswordTextField passwordField1 = (PasswordTextField)
form.get("password1Field");
        PasswordTextField passwordField2 = (PasswordTextField)
form.get("password2Field");

        // Fill them with appropriate data.
        codeField.setDefaultModelObject(originalGebruiker.getCode());
        nameField.setDefaultModelObject(originalGebruiker.getNaam());
       
organisationField.setDefaultModelObject(originalGebruiker.getOrganisatie());
       
passwordField1.setDefaultModelObject(originalGebruiker.getWachtwoord());
        passwordField2.setDefaultModelObject("");
    }

    private void changeState(PageState newState) {
        state = newState;
        // Now change state of some fields based on page state.
        Form form = (Form) get("form");
        TextField codeField = (TextField) form.get("codeField");
        if (state == PageState.EDIT) {
            codeField.setEnabled(false);
        } else {
            codeField.setEnabled(true);
        }
    }

    private void processSearch() {
        // Goto the search page.
        setRedirect(false);
        setResponsePage(OnderhoudGebruikerSearchPage.class);
    }

    private void processDelete() {
        // TODO
    }

    private void processNew() {
        originalGebruiker = new AutorisatieGebruiker("", "", "", "");
        changeState(PageState.NEW);
        resetData();
    }

    private void processSave() {
        // TODO
    }

    private void processCancel() {
        resetData();
    }
}

It would help if you could give me some insight about the reason i cannot
use a button and why the principle i use fails.

Thanks in advance.

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/How-can-i-reset-the-data-in-TextFields-tp3625099p3625841.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org