Tapas Bose
Tapas Bose

Reputation: 29816

Submit Form on Enter press - Textfield value is Null

In my login page I am trying to bind enter key press event with password textfield. I have managed to fire the event listener. But the problem I am facing is the value of the textfield username is returning null, but the password field's value is not null. The code is given below:

public class LoginForm extends Form<Void> {
    private transient final ValueMap properties = new ValueMap();

    public LoginForm(final String formId) {
        super(formId);

        final TextField<String> username = new TextField<String>("username", new PropertyModel<String>(getProperties(), "username"));
        add(username);

        final TextField<String> password = new TextField<String>("password", new PropertyModel<String>(getProperties(), "password"));
        add(password);

        password.add(new AjaxFormComponentUpdatingBehavior("onkeypress") {

            @Override
            protected void onComponentTag(ComponentTag tag) {
                Component component = getComponent();
                if (component.isEnabled() && component.isEnableAllowed()) {
                    CharSequence handler = generateCallbackScript(new AppendingStringBuffer("wicketAjaxPost('").append(getCallbackUrl()).append("', wicketSerialize(Wicket.$('" + getComponent().getMarkupId() + "'))"));
                    String event = "if (event.keyCode == 13) {" + handler.toString() + "};";
                    tag.put("onkeypress", event);
                }
            }

            @Override
            protected void onUpdate(AjaxRequestTarget target) {                 
                System.out.println(getUsername());
                System.out.println(getPassword());
            }
        });
    }

    public ValueMap getProperties() {
        return properties;
    }

    private String getPassword() {
        return getProperties().getString("username");
    }

    private String getUsername() {
        return getProperties().getString("password");
    }
}

The system.out is printing

null
password

I also tried to use username.getDefaultModelObject() but that is also returns null.

Any information will be very helpful to me.

Thanks.

Upvotes: 0

Views: 1987

Answers (1)

bert
bert

Reputation: 7696

You will need to submit your LoginForm via Javascript. This would trigger the usual Wicket form processing and store the values in the models. You should be able to submit a form via:

document.getElementById('myform').submit(); 

But, I have not tested it.

Upvotes: 1

Related Questions