Drani Academy – Interview Question, Search Job, Tuitorials, Cheat Sheet, Project, eBook

Angular JS

Tutorials – Angular JS

 
Chapter 8: Working with Forms in AngularJS

 

Forms are a fundamental part of web applications, and AngularJS simplifies the process of creating and handling forms. In this chapter, we will explore how to work with forms in AngularJS, including form creation, validation, submission, and handling user input.

Introduction to AngularJS Forms

Forms are essential for collecting data from users and enabling interactions in web applications. AngularJS provides a set of powerful features and directives to simplify form handling and validation. Whether you’re building a simple contact form or a complex data entry system, AngularJS makes it easier to work with forms.

AngularJS Form Directives

AngularJS provides a collection of directives specifically designed for working with forms. Some of the key form-related directives include:

  • ngForm: A directive that allows you to create a form element.
  • ngModel: A directive used to bind form input elements to properties in the model.
  • ngSubmit: A directive for submitting a form when a button is clicked.
  • ngRequired, ngMinlength, ngMaxlength: Directives for form field validation.
  • ngPattern: A directive for enforcing a specific pattern in form inputs.
  • ngChange: A directive to trigger a function when an input field changes.

Creating a Form

To create a form in AngularJS, you need to use the ngForm directive to define a form element. You can use the name attribute to give your form a unique name, and then use ngModel to bind form fields to properties in your model.

Here’s an example of a simple AngularJS form:

<form name="myForm" ng-submit="submitForm()">
    <label>Name:</label>
    <input type="text" name="name" ng-model="user.name" required>
    <label>Email:</label>
    <input type="email" name="email" ng-model="user.email" required>
    <button type="submit">Submit</button>
</form>

In this example, we’ve created a form named “myForm” using the ngForm directive. We’ve also used the ngModel directive to bind the name and email input fields to properties in the user object. The required attribute is used to make these fields mandatory.

Form Validation

AngularJS provides various built-in validation directives to ensure data entered into the form is valid. Common validation directives include ngRequired, ngMinlength, ngMaxlength, and ngPattern. These directives allow you to specify validation rules for form fields.

Here’s an example of using validation directives:

<form name="myForm" ng-submit="submitForm()">
    <label>Name:</label>
    <input type="text" name="name" ng-model="user.name" required>
    <label>Email:</label>
    <input type="email" name="email" ng-model="user.email" required>
    <label>Password:</label>
    <input type="password" name="password" ng-model="user.password" ng-minlength="8" ng-maxlength="20" required>
    <button type="submit">Submit</button>
</form>

In this example, the name and email fields are marked as required using the required attribute. The password field has minimum and maximum length requirements specified using the ng-minlength and ng-maxlength directives.

Displaying Validation Errors

AngularJS makes it easy to display validation error messages to users. You can use the ngMessages directive to conditionally display error messages based on validation rules.

Here’s an example of displaying validation error messages:

<form name="myForm" ng-submit="submitForm()">
    <label>Name:</label>
    <input type="text" name="name" ng-model="user.name" required>
    <div ng-messages="myForm.name.$error">
        <div ng-message="required">Name is required.</div>
    </div>
    <label>Email:</label>
    <input type="email" name="email" ng-model="user.email" required>
    <div ng-messages="myForm.email.$error">
        <div ng-message="required">Email is required.</div>
    </div>
    <label>Password:</label>
    <input type="password" name="password" ng-model="user.password" ng-minlength="8" ng-maxlength="20" required>
    <div ng-messages="myForm.password.$error">
        <div ng-message="required">Password is required.</div>
        <div ng-message="minlength">Password is too short.</div>
        <div ng-message="maxlength">Password is too long.</div>
    </div>
    <button type="submit">Submit</button>
</form>

In this example, the ngMessages directive is used to display error messages for the name, email, and password fields based on the validation rules.

Form Submission

AngularJS provides the ngSubmit directive to handle form submission. You can attach the ng-submit directive to a button within the form, and specify a function to be executed when the form is submitted.

Here’s an example of form submission:

<form name="myForm" ng-submit="submitForm()">
    <!-- Form fields and validation here -->
    <button type="submit">Submit</button>
</form>

In this example, the submitForm function is called when the form is submitted. You can define this function in your controller to handle form data and perform actions such as sending data to a server.

Working with User Input

AngularJS makes it easy to work with user input in forms. You can use the ngModel directive to bind form fields to properties in your model, and changes to form fields are automatically reflected in the model.

<form name="myForm" ng-submit="submitForm()">
    <label>Name:</label>
    <input type="text" name="name" ng-model="user.name" required>
    <label>Email:</label>
    <input type="email" name="email" ng-model="user.email" required>
    <button type="submit">Submit</button>
</form>

In this example, changes to the name and email fields are automatically reflected in the user.name and user.email properties.

Form Reset

AngularJS provides a simple way to reset form fields and clear form validation. You can use the ng-click directive to create a button that resets the form.

<form name="myForm" ng-submit="submitForm()">
    <!-- Form fields and validation here -->
    <button type="reset" ng-click="resetForm()">Reset</button>
</form>

In this example, the resetForm function is called when the “Reset” button is clicked. You can define this function in your controller to clear form data and validation.


Conclusion

Working with forms in AngularJS is straightforward and powerful. AngularJS provides a set of directives and tools to create, validate, and submit forms easily. Understanding how to bind form fields to your model, handle form validation, and work with user input is essential for building interactive and user-friendly web applications.

In the next chapter, we will dive into directives, a core feature of AngularJS that extends HTML with custom behavior and enhances the interactivity of your web applications.

Scroll to Top