25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 7 ■ USING THE <strong>HTML</strong>5 FORMS API<br />

Let’s break that code snippet down a bit.<br />

First, we declare a handler to receive invalid events. The first thing we do inside that handler is<br />

check the source of the event. Recall that the invalid event is fired on the form control with a validation<br />

error. Therefore, the srcElement of the event will be the misbehaving form control.<br />

From the source, we grab the validity object. Using this ValidityState instance, we can check its<br />

individual constraint fields to determine exactly what went wrong. In this case, since we know that our<br />

field has a required attribute on it, we first check to see if the valueMissing constraint has been violated.<br />

If our check succeeds, we can modify the user interface on the page to inform the user that a value<br />

needs to be entered for the field in error. Perhaps an alert or an informative error region could be<br />

displayed? This is up to you to decide.<br />

Once we’ve told the user what the error is and how to correct it, we need to decide if we want the<br />

browser itself to display its built-in feedback. By default, the browser will do just that. To prevent the<br />

browser from showing its own error message, we can call evt.preventDefault() to stop the default<br />

handling and take care of the matter entirely ourselves.<br />

Once again, the choice here is yours. The <strong>HTML</strong>5 Forms API provides you with the flexibility to<br />

achieve a customized API or to fall back to default browser behavior.<br />

Turning Off Validation<br />

In spite of the power behind the validation API, there are… (ahem) valid reasons why you might want to<br />

turn off validation on a control or an entire form. The most common reason is that you might choose to<br />

submit the temporary contents of a form to be saved or retrieved for later, even if the contents aren’t<br />

quite valid yet.<br />

Imagine the case of a user who is entering a complex order entry form, but needs to run an errand<br />

midway through the process. Ideally, you might present the user with a “save” button which stores the<br />

values of the form by submitting them to the server. However, if the form was only partially completed,<br />

validation rules might prevent the content from being submitted. The user would be very displeased if<br />

she had to complete or abandon the form due to an unexpected interruption.<br />

To handle this, a form itself can be programmatically set with the attribute noValidate, which will<br />

cause it to forego any validation logic otherwise present and simply submit the form. Naturally, this<br />

attribute can be set either via script or original markup.<br />

A more useful way to turn off validation is to set a formNoValidate attribute on a control such as a<br />

form submit button. Take the following submit button, set up as a “save” button, for example:<br />

<br />

<br />

This snippet will create a two normal looking submit buttons. The second will submit the form, as<br />

usual. However, the first button is marked with the noValidate attribute, causing all validation to be<br />

bypassed when it is used. This allows the data to be submitted to the server without checking for<br />

correctness. Of course, your server will need to be set up to handle unvalidated data, but best practices<br />

dictate that this should be the case at all times.<br />

Building an Application with <strong>HTML</strong>5 Forms<br />

Now, let’s use the tools we’ve described in this chapter to create a simple signup page which showcases<br />

new features in <strong>HTML</strong>5 Forms. Turning back to our familiar Happy Trails Running Club, we’ll create a<br />

page for race registration that incorporates new form elements and validation.<br />

185

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!