Getting started with this object is simple. First, we need to declare it and then call its initialize method as shown in Listing 12.1.
Listing 12.1. Creating and Initializing the UserValidator Object (UserValidator.js)
Declaring a Singleton object is nothing new, but we do have quite a few local object properties that are unique to this object and set within its initialize method. The first is a property called currentSelector, which will be used in our validate method to hold the current form field id that is being validated. The second is the currentForm property, which will be used to hold an id for the current form that is being validated. The third is the mode property, which will be used to check whether the page is in login or registration mode. The fourth is called fieldNumToValidate and will be used to hold a number that indicates how many form elements need to be validated before the Register button is enabled for a user to register. The last property is called submitId, which simply holds the id of the Submit button for the current form and will be used in the setMode method to change the value from login to register if a new user clicks the Register link. All of these properties will allow us create a form that interacts with the user. The initialize method is fired when the body in our login/registration page loads. Listing 12.2 shows how this method is added to our page.
Listing 12.2. Initializing the UserValidator Object (index.php)
We are passing the values for the object properties as parameters of this method when the page loads.
The next method that we will create sets the mode for the form, switching it from login to registration in our case (see Listing 12.3). This method is called setMode and it takes one parameter, which is the new mode value. In our sample, this value will be 'Register'.
Listing 12.3. Setting the Mode from Login to Register (UserValidator.js)
This method takes the new mode value and sets the local mode property, which we created in the intialize method, and the value of the Submit button to this new value. It then disables the Submit button until the form is validated based on the fieldNumToValidate property that we set in the initialize method and appends the mode value to the current form's action URL. Appending this value allows us to retrieve it when the post-back happens and use it to decipher whether we need to register a new user or log in an existing one.
Validating User Input
In order to validate the two fields that we specified in the intialize method, we have to create a validate method that takes a selector parameter and its value. The selector parameter is the id for the element that is being validated and the value parameter is the value of this element. Listing 12.4 shows this completed method, which we will cover in a moment.
Listing 12.4. Validating Forms Against Database Data (UserValidator.js)
Now that you have taken a look at the code, we can cover it line by line. The first thing we do is check to make sure that neither of the parameters is empty. If they are not empty, we set the currentSelector to the selector parameter and create a url variable to pass through an Ajax request. The url variable that we create consists of a serviceConnector.php file, an object parameter that is set to the userManager object that we will create soon, and a method parameter that is set to a verifyUserInformation method. The last part of the query is called param and is set to the selector and value parameters that were passed into the object. The parameters are concatenated into a comma-delimited list and the url variable is finally sent to the server through an Ajax request with the onValidation method set as the callback. Of course, there is always the possibility that the selector could be set, but the value parameter could be an empty string, so we have a condition that sets the selector element's visual feedback to the default state, just in case a user has already validated the element already.
In order to call this validation method from the client page, we need to add the code to the onblur for the form elements we want to validate. Listing 12.5 shows the body of the index.php sample page.
Listing 12.5. Calling the Validation Method from the Form (index.php)
These validation calls are really simple and can be duplicated in any element that we want to add them to because they are getting their parameters from the current element to which they are attached. The only requirement is that there is an id attribute for the element that is calling the method. There is PHP code in the action that makes the form post back on this same page, which will require us to handle the login and registration within this page. It also has a PHP $error message that is displayed in this page if an error exists. This message will be covered in the next section when we add more PHP code to the top of this file to handle the login and registration.
Providing Visual Feedback
Visual feedback is important in web applications and often overlooked by developers. Adding visual feedback keeps users in touch with their interactions and does not leave them wondering what has happened to their data when they have clicked a button, exited a form field, and so on. Providing feedback with our UserValidator object is not difficult to accomplish because all we need to do is match element ids and change styles based on responses from our XHRs. Listing 12.6 shows this code and how it handles displaying visual clues to the user.
Listing 12.6. Providing Visual Feedback to Users (UserValidator.js)
There are quite a few conditions in this method, but don't be intimidatedthe code is very simple. The first thing we check is the ready state of the Ajax object, which we have already covered in the other components that we have created. After it verifies, we check the response to see if a success or failed message was returned from our server-side object, which we will be creating shortly. If the response is successful, it means we need to set the form element background to red and add a message stating that the data is already in use because this data already exists in the database. Otherwise, we set it to green and add an 'OK' message. We also update the fieldNumToValidate property based on the condition of the response. The last thing we check is whether the fieldNumToValidate is equal to zero and, if it is, we enable the button.
Now that we have created the client side of the request, we need to check the database and respond with the XML based on matching data.