JustMaier / angular-autoFields-bootstrap

Automatically generate fields from a schema object and bind them to an object
MIT License
208 stars 42 forks source link

angular-autofields-bootstrap

Nuget version NPM version

Avoid bloating your templates with repetitive form html.
Instead, just specify a schema for the form and the model you want to bind it to and you're done!

Check out a demo & documentation

Installation

Nuget

install-package AngularJs.AutoFields.Bootstrap

Manually

<script type="text/javascript" src="https://github.com/JustMaier/angular-autoFields-bootstrap/raw/master/js/autofields.js"></script>
<!-- with bootstrap -->
<script type="text/javascript" src="https://github.com/JustMaier/angular-autoFields-bootstrap/raw/master/js/autofields-bootstrap.js"></script>

Usage

  1. If you're doing this manually and using bootstrap, be sure to install Angular-UI Bootstrap for date popover support
  2. Include the autofields.js script provided by this component into your app. If you are using bootstrap, also include autofields-bootstrap.js
  3. add autofields as a module dependency to your app

Javascript

angular.module('app',['autofields'])
.controller('JoinCtrl', ['$scope', function ($scope) {
    $scope.user = {
        username: '',
        password: '',
        confirmPassword: '',
    };

    $scope.schema = [
        { property: 'username', type: 'text', attr: { ngMinlength: 4, required: true }, msgs: {minlength: 'Needs to have at least 4 characters'} },
        { property: 'password', type: 'password', attr: { required: true } },
        { property: 'confirmPassword', label: 'Confirm Password', type: 'password', attr: { confirmPassword: 'user.password', required: true } }
    ];

    $scope.join = function(){
        if(!$scope.joinForm.$valid) return;
        //join stuff....
    }
}]);

Html

 <form name="joinForm" ng-submit="join()">
    <auto:fields fields="schema" data="user"></auto:fields>
    <button type="submit" class="btn btn-default btn-lg btn-block" ng-class="{'btn-primary':joinForm.$valid}">Join</button>
</form>

Field Schema

Options

Standard

With Validation

With Bootstrap

Extend

AutoFields is now highly extensible allowing developer to easily add new field types and field properties.

Adding New Field Types

$autofieldsProvider.registerHandler(types, handler)

Example

module('autofields.checkbox', ['autofields.core'])
.config(['$autofieldsProvider', function($autofieldsProvider){
    // Checkbox Field Handler
    $autofieldsProvider.registerHandler('checkbox', function(directive, field, index){
        var fieldElements = $autofieldsProvider.field(directive, field, '&lt;input/&gt;');

        if(fieldElements.label) fieldElements.label.prepend(fieldElements.input);

        return fieldElements.fieldContainer;
    });
}]);

Adding New Field Properties

$autofieldsProvider.registerMutator(key, mutator, options)

Example

module('autofields.helpblock', ['autofields.core'])
.config(['$autofieldsProvider', function($autofieldsProvider){
    // Help Block Propert Support
    $autofieldsProvider.registerMutator('helpBlock', function(directive, field, fieldElements){
        if(!field.help) return fieldElements;

        fieldElements.helpBlock = angular.element('&lt;p/&gt;');
        fieldElements.helpBlock.addClass(directive.options.classes.helpBlock.join(' '))
        fieldElements.helpBlock.html(field.help);
        fieldElements.fieldContainer.append(fieldElements.helpBlock);

        return fieldElements;
    });
}]);

Notes