In this tutorial, we want to build a simple login form containing a text field for the user name and a password field for the password. Additionally, a button for the login is needed.
Forms are abstract objects in qooxdoo which hold the input fields and buttons. As we need two input fields we should create them as well. After that, we can add these input fields to the form, as you can see in the following code snippet.
var form = new qx.ui.form.Form(); var userName = new qx.ui.form.TextField(); form.add(userName, "Name"); var password = new qx.ui.form.PasswordField(); form.add(password, "Password");
Running this code creates the form, but does not show anything in the app. As described above, forms are abstract objects and do not contain any information on how to layout the form on the screen. For this, a form renderer can be used.
Take a simple, one column renderer and render the form.
this.getRoot().add( new qx.ui.form.renderer.Single(form), {left: 10, top: 10} );
Running these two code snippets will render a simple form showing the two input fields. You can also see labels in front of each form item showing the name we gave it with the add
call. Give it a try and enter some values or even rename the labels in the code and run it again.
As a next step, we want to add the login button. For adding buttons, the form offers a separate method, which will be used in the following snippet.
var login = new qx.ui.form.Button("Login"); form.addButton(login);
Copy this code in front of the form rendering to make sure the renderer knows about the button. Run and see the new added Login button.
But the button does not do much right now. We should add a listener to validate the form and send the login request. Of course, sending data would stretch this tutorial a bit too much so we simply show an alert.
login.addListener("execute", function() { if (form.validate()) { alert("send..."); } });
Appending these code lines to the bottom of the whole sample will attach a listener to the button, which will be executed on click but also on pressing enter when the button is focused. As you can see in the code, we only show the alert in case the validation reported back as valid. Of course, as we did not add any validation information, everything is valid currently!
We use two simple cases, the user name is required and the password field should at least have 6 characters.
Making the user name required is really easy. Just tell the user name object to be required.
userName.setRequired(true);
Adding this line right after creating the text field will mark the field as required. Running the code now will show you a little red asterisk right beside the name of the field which marks it as required. Hitting the login button now without a user name will fail and highlight the input. Hovering will show a pop-up containing the error message.
Adding a custom validator is also easy. Search for the line where we added the password field to the form and add a function as third parameter. This function will be your validator. The validator will have access to the current set value as its first argument. The new line could look like this.
form.add(password, "Password", function(data) { return !!(data && data.length >= 6); });
After you have replaced the original add
call with the new one, give it a try and see that also the password field needs to be filled. Hovering the field does not show an error message, but we can add one in a single line of code.
password.setInvalidMessage("Password too short.");
Adding this line after the password field fixes this issue.