concept validation in category aurelia

appears as: validation, validation
Aurelia in Action

This is an excerpt from Manning's book Aurelia in Action.

With the Aurelia-validation plugin installed and registered with your Aurelia application, you can now put it to use by adding validation to the edit-book form.

Forms are one of the primary ways that users interact with your website. Well-implemented forms make users happy. Conversely, poorly implemented forms can slow down or stop user activity on your site. You can address two of the main aspects of well-built forms using Aurelia’s binding and validation components. Binding can be used to provide rapid feedback, guiding the user on the correct form journey by making their next action obvious. Validation can be used to show users the feedback they need to submit the form in a way that’s unobtrusive and avoids breaking user flow. In this chapter, you’ll learn how to use the main HTML form elements that you’re used to, such as check boxes, radio elements, select controls, and text input, with Aurelia’s binding system. You’ll then learn how to add validation to your Aurelia forms using Aurelia’s validation plugin.

7.5.3. Adding validation to the edit-books form

Adding validation to an Aurelia application is achieved using the following steps:

Step 4: Marking input elements for validation

The last step of the validation setup process is to mark the title and times-read input fields for validation. First, add the new times-read input field with a two-way binding to the timesRead property on the temporaryBook model. With this in place, you can apply the validate binding behavior to the value.bind="temporaryBook.title & validate" and value.bind="temporaryBook.timesRead & validate"/> binding expressions. The validate binding behavior generates validation triggers (such as input change and blur) that the validation controller (set up in step 2) responds to.

Listing 7.17. Marking the books view fields for validation (edit-book.html)
<template>
    ...
    <div ref="editFormDiv" ... >
        <div class="wrapper">
            ...
            <form>
                <div class="form-group row">
                    <label for="title">Title</label>
                    <input type="text"
                          class="form-control"
                          id="title"
                          value.bind="temporaryBook.title
                                & validate"                            #1
                          placeholder="book title">
                </div>
                ...
                <div class="form-inline row">
                    <div id='own-a-copy'... >
                       ...
                    </div>
                    <div class="form-group">                           #2
                        <label for="times-read"                        #2
                               class="mb-2 mr-sm-2 mb-sm-0">           #2
                                Times Read                             #2
                        </label>                                       #2
                        <input name="times-read"                       #2
                               class="form-control                     #2
                                      number                           #2
                                      mb-2 mr-sm-2                     #2
                                      mb-sm-0"                         #2
                               placeholder="0"                         #2
                               value.bind="temporaryBook.timesRead     #2
                                    & validate"/>                      #2 #3
                    </div>                                             #2
                </div>
            </form>
        </div>
    </div>
</template>
Figure 7.9. Title and Times Read input fields marked for validation using the validate binding behavior

After setting up validation on the edit-books form, you should understand the core components that make up Aurelia’s validation system. These components can be combined in numerous ways to implement any validation requirements you might come across. The aurelia-validation package is powerful.