Giter VIP home page Giter VIP logo

ng-drf-form-field's Introduction

ng-drf-form-field

An AngularJS form field directive that uses Django Rest Framework's metadata in order to generate the field with the proper validation, input type, choices, etc.

Usage example:

controller.js

'use strict';

angular.module('mainApp')
    .controller('ParentCtrl', [
        '$scope',
        'ResourceForm',
        function ($scope, ResourceForm) {

            // get the meta data from server
            var promise = ResourceForm.options('RESOURCE_URL');

            // populate
            $scope.metadata = {};
            promise.then(function(response) {
                $scope.metadata = response.data.actions.POST;
            });

            // this object will be used to read from/write to 
            $scope.formModel = {
                title: 'Mega mouse',
                duration: 0,
                active: false
            };
        }
]);

service.js - responsible for getting the meta data, returns a promise 'use strict';

angular.module('mainApp')
    .service('ResourceForm', [
        '$http',
        function ResourceForm($http) {
            this.options = function(url) {
                var promise = $http({method: 'OPTIONS', url: url});
                return promise;
            };
        }
]);

In view you can use , three parameters are required

  • metadata -- that's the data object received from the server describing how each field look like
  • key -- tells the directive what field it should render (title, username, duration etc.)
  • ng-model="formModel" -- tells the directive what object should be used for storing the input values (hardcoded to formModel)

If you creating a Label element, ng-model should be skipped.

View

<form name="movieForm" method="POST" novalidate>

    <div class="form-group" ng-class="{ 'has-error': !movieForm.title.$valid }">
        <!-- A label -->
        <form-field label metadata="metadata" key="title" class="col-sm-2 control-label"></form-field>
        <div class="col-sm-10">
            <!-- An input -->
            <form-field metadata="metadata" key="title" class="form-control" id="title" ng-model="formModel" name="title"></form-field>
            <!-- Form validation -->
            <p ng-show="movieForm.title.$error.maxlength" class="help-block">Title is too long</p>
            <p ng-show="movieForm.title.$error.required" class="help-block">Title is required</p>
        </div>
    </div>

</form>

ng-drf-form-field's People

Contributors

purplebit avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.