Nishchay Saxena
Nishchay Saxena

Reputation: 1

Onsubmit of form, page get refreshed and PageParameters gets empty in wicket

In LoginPage.java, I have added some PageParameters and redirect the user to another page.

PageParameters parameters = new PageParameters();
parameters.add("firstName", firstNameValue);
parameters.add("lastName", lastNameValue);
parameters.add("emailAddress", emailAddressValue);

throw new RedirectToPageException(UserInformationPage.class, parameters);

In AdditionalInformationPage.java

public class UserInformationPage extends BaseWebPage {
    private static final long serialVersionUID = 3614392599102678526L;

    public UserInformationPage(final PageParameters parameters) {
        super(parameters);
        setOutputMarkupId(true);

        String firstName, lastName, emailAddress;

        firstName = parameters.get("firstName").toOptionalString();
        lastName = parameters.get("lastName").toOptionalString();
        emailAddress = parameters.get("emailAddress").toOptionalString();

        WebMarkupContainer userInformationPageWrapper = new WebMarkupContainer("userInformationPageWrapper");
        userInformationPageWrapper.add(new UserInformationPanel("userInformationPageContent", firstName, lastName, emailAddress));
        add(userInformationPageWrapper.setMarkupId("userInformationPageWrapper"));
    }
}

UserInformationPanel.java

public class UserInformationPanel extends Panel {
    private static final long serialVersionUID = -1016518626600751985L;

    public UserInformationPanel(String id, String idpUuid, firstName, lastName, emailAddress) {
        super(id);
        setOutputMarkupId(true);

        Form<Void> userInformationForm = new CSRFSafeForm<Void>("userInformationForm") {
            private static final long serialVersionUID = 2633350725131958527L;

            @Override
            protected void onConfigure() {
                super.onConfigure();
                setVisible(true);
            }
        };

        FeedbackPanel errorFeedbackPanel = new TrackedFeedbackPanel("errorFeedback", new ErrorLevelFeedbackMessageFilter(FeedbackMessage.ERROR));
        errorFeedbackPanel.setMaxMessages(MAX_ERROR_MESSAGES);
        userInformationForm.add(errorFeedbackPanel.setOutputMarkupId(true));

        TextField<String> firstName = new TextField<>("firstName", firstName);
        firstName.add(StringValidator.maximumLength(DatabaseConstants.User.FIRST_NAME_MAX_LENGTH));
        userInformationForm.add(firstName.setRequired(true).setEnabled(true));

        TextField<String> lastName = new TextField<>("lastName", lastName));
        lastName.add(StringValidator.maximumLength(DatabaseConstants.User.LAST_NAME_MAX_LENGTH));
        userInformationForm.add(lastName.setRequired(true).setEnabled(true));

        EmailAddressValidator emailAddressValidator = EmailAddressValidator.getInstance();
        TextField<String> emailAddress = new EmailTextField("emailAddress", emailAddress), emailAddressValidator);
        emailAddress.setRequired(false)
                .add(UniqueEmailValidator.getInstance(UniqueEmailValidator.ErrorMsgType.REGISTER))
                .add(StringValidator.maximumLength(DatabaseConstants.EMAIL_ADDRESS_MAX_LENGTH));
        emailAddress.setEnabled(false);
        userInformationForm.add(emailAddress);

        userInformationForm.add(new AjaxButton("submitButton") {
            private static final long serialVersionUID = -1723378347103997463L;

            @Override
            public void onSubmit(AjaxRequestTarget target, Form<?> form) {
                super.onSubmit(target, form);
                Map<String, Object> userAttributes = new LinkedHashMap<>();
                userAttributes.put("email_Address", emailAddress);
                userAttributes.put("first_Name", firstName);
                userAttributes.put("last_Name", lastName);
                // logic to save userAttributes in DB.
                throw new RedirectToUrlException("/home");
            }

            @Override
            protected void onError(AjaxRequestTarget target, Form<?> form) {
                super.onError(target, form);
                target.add(errorFeedbackPanel);
            }
        });
        userInformationForm.setOutputMarkupId(true);
        add(userInformationForm);
    }
}

Button in html file like this:

<button wicket:id="submitButton" type="submit" class="adb-button__primary nextStep">
                    <span><wicket:message key="Submit"/></span>
                </button>

First time page gets rendered successfully. Fields get pre-populated. When I click on submit buttton first time, the page gets refreshed and page parameters gets empty. Form is rendered again but with empty values. On second click it works properly.

So, How can I stop page refresh so that pageParams does not gets empty and on click of submit button in first time, it validates the form and show error if any on page?

Upvotes: 0

Views: 285

Answers (1)

Zak Aroui
Zak Aroui

Reputation: 31

I couldn't add a comment so I am suggesting this answer.

Please check this line : throw new RedirectToUrlException("/home"); inside the onSubmit() method. Why are you using it? This means that you are redirecting to /home whenever the user submits the form, and probably at home your are redirecting back to the LoginPage. Please check what is happening at the /home route, or post more information so I can help you better.

Upvotes: 1

Related Questions