Home

Awesome

angular-async-form

Async form handling the angular way.

Bower version NPM version Downloads Build Status Coveralls Status Awesome

If you've developed angular apps you know how difficult it is to handle errors after submitting a form. Common scenarios include:

Angular provides a great set of directives if your only concern is immediate validation in the UI; however, they fall short for anything else.

angular-async-form (namespaced af) fills this gap with the following directives:

You can customize it's behavior with afConfig.

Highlights

Full Example

The directives are used in concert as follows:

<!DOCTYPE html>
<html ng-app="myApp">
  <head>
    <style>
    .error {color: red;}
    </style>
  </head>
  <body ng-controller="AppCtrl">
    <h1>Hello {{ user.firstName }}!</h1>

    <form af-submit="doSomething($event, cb)" novalidate>
      <div class="error" af-message>{{ message }}</div>
      <div class="control-group" af-control-group>
        <div class="error" af-control-message>{{ error }}</div>

        <input
          name="firstName"
          ng-model="user.firstName"
          af-control
          required>

        <!-- doSomething($event, cb) is called.  It's up to doSomething to warn the
        user by calling cb if errors occured in async operations.  If no errors
        occurred, then doSomething can do something else like hide the form and load
        a new view. -->
        <button type="submit">Submit</button>
      </div>
    </form>

    <script src="https://code.angularjs.org/1.4.8/angular.js"></script>
    <script src="angular-async-form.js"></script>
    <script src="app.js"></script>
  </body>
</html>

Directive API

afSubmit

RestrictionRequiresScope
AformParent Scope (afSubmit must equal an angular expression)

Use afSubmit as a direct replacement for ngSubmit. In addition to the $event object, a callback is exposed as cb. cb has the following signature:

function (message, errors)

afMessage

RestrictionRequiresScope
AE^^afSubmitChild Scope provides message

Adds a message to the child scope of the element it is used on. The message is provided by passing a value as the message parameter in the afSubmit callback function.

afControlGroup

RestrictionRequiresScope
AE?^^afSubmitParent Scope

Groups a form control with a corresponding message. This allows error messages passed to the errors parameter in afSubmit to apply to their intended control, or group of controls in the case of radio buttons and checkboxes. Multiple afControls must have the same name.

afControlMessage

RestrictionRequiresScope
AE^^afControlGroupChild Scope provides error

Adds a message to a form control group. If the control received an error via the errors parameter to the callback in afSubmit then it will be provided in this directive's scope.

afControl

RestrictionRequiresScope
A^^afControlGroup, ngModelParent Scope (name is a required attribute)

Adds a form control to a form control group. The name attribute is used as the key when supplying errors to the callback function in afSubmit. This directive may be used any number of times within afControlGroup (I.E. with radio inputs). If this directive does not exist within afControlGroup, then no error will display in afControlMessage.

Errors must be resolved by triggering a blur event on the control; otherwise, successive evaluations of afSubmit will be blocked.

Configuration

You can customize angular-async-form by using afConfig.

afConfig

A simple object with the following properties:

License

The MIT License (MIT)

Copyright (c) 2015 Kogo Software LLC

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.