Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 1 | /** |
| 2 | * © OpenCORD |
| 3 | * |
| 4 | * Visit http://guide.xosproject.org/devguide/addview/ for more information |
| 5 | * |
| 6 | * Created by teone on 4/15/16. |
| 7 | */ |
| 8 | |
| 9 | (function () { |
| 10 | 'use strict'; |
| 11 | |
| 12 | angular.module('xos.uiComponents') |
| 13 | |
| 14 | /** |
| 15 | * @ngdoc directive |
| 16 | * @name xos.uiComponents.directive:xosValidation |
| 17 | * @restrict E |
| 18 | * @description The xos-validation directive |
| 19 | * @param {Object} errors The error object |
| 20 | * @element ANY |
| 21 | * @scope |
| 22 | * @example |
| 23 | <example module="sampleValidation"> |
| 24 | <file name="index.html"> |
| 25 | <div ng-controller="SampleCtrl as vm"> |
| 26 | <div class="row"> |
| 27 | <div class="col-xs-12"> |
| 28 | <label>Set an error type:</label> |
| 29 | </div> |
| 30 | <div class="col-xs-2"> |
| 31 | <a class="btn" |
| 32 | ng-click="vm.field.$error.required = !vm.field.$error.required" |
| 33 | ng-class="{'btn-default': !vm.field.$error.required, 'btn-success': vm.field.$error.required}"> |
| 34 | Required |
| 35 | </a> |
| 36 | </div> |
| 37 | <div class="col-xs-2"> |
| 38 | <a class="btn" |
| 39 | ng-click="vm.field.$error.email = !vm.field.$error.email" |
| 40 | ng-class="{'btn-default': !vm.field.$error.email, 'btn-success': vm.field.$error.email}"> |
| 41 | Email |
| 42 | </a> |
| 43 | </div> |
| 44 | <div class="col-xs-2"> |
| 45 | <a class="btn" |
| 46 | ng-click="vm.field.$error.minlength = !vm.field.$error.minlength" |
| 47 | ng-class="{'btn-default': !vm.field.$error.minlength, 'btn-success': vm.field.$error.minlength}"> |
| 48 | Min Length |
| 49 | </a> |
| 50 | </div> |
| 51 | <div class="col-xs-2"> |
| 52 | <a class="btn" |
| 53 | ng-click="vm.field.$error.maxlength = !vm.field.$error.maxlength" |
| 54 | ng-class="{'btn-default': !vm.field.$error.maxlength, 'btn-success': vm.field.$error.maxlength}"> |
| 55 | Max Length |
| 56 | </a> |
| 57 | </div> |
| 58 | </div> |
| 59 | <xos-validation field ="vm.field" form = "vm.form"></xos-validation> |
| 60 | </div> |
| 61 | </file> |
| 62 | <file name="script.js"> |
| 63 | angular.module('sampleValidation', ['xos.uiComponents']) |
| 64 | .controller('SampleCtrl', function(){ |
| 65 | this.field = { |
| 66 | $error: {} |
| 67 | }; |
| 68 | this.form= { |
| 69 | $submitted:true |
| 70 | } |
| 71 | }); |
| 72 | </file> |
| 73 | </example> |
| 74 | */ |
| 75 | |
Arpit Agarwal | 34b6383 | 2016-08-08 11:59:45 -0700 | [diff] [blame] | 76 | .component('xosValidation', { |
| 77 | restrict: 'E', |
| 78 | bindings: { |
| 79 | field: '=', |
| 80 | form: '=' |
| 81 | }, |
| 82 | template: ` |
| 83 | <div ng-cloak> |
| 84 | <xos-alert config="vm.config" show="vm.field.$error.required !== undefined && vm.field.$error.required !== false && (vm.field.$touched || vm.form.$submitted)"> |
| 85 | Field required |
| 86 | </xos-alert> |
| 87 | <xos-alert config="vm.config" show="vm.field.$error.email !== undefined && vm.field.$error.email !== false && (vm.field.$touched || vm.form.$submitted)"> |
| 88 | This is not a valid email |
| 89 | </xos-alert> |
| 90 | <xos-alert config="vm.config" show="vm.field.$error.minlength !== undefined && vm.field.$error.minlength !== false && (vm.field.$touched || vm.form.$submitted)"> |
| 91 | Too short |
| 92 | </xos-alert> |
| 93 | <xos-alert config="vm.config" show="vm.field.$error.maxlength !== undefined && vm.field.$error.maxlength !== false && (vm.field.$touched || vm.form.$submitted)"> |
| 94 | Too long |
| 95 | </xos-alert> |
| 96 | <xos-alert config="vm.config" show="vm.field.$error.custom !== undefined && vm.field.$error.custom !== false && (vm.field.$touched || vm.form.$submitted)"> |
| 97 | Field invalid |
| 98 | </xos-alert> |
| 99 | </div> |
| 100 | `, |
| 101 | transclude: true, |
| 102 | bindToController: true, |
| 103 | controllerAs: 'vm', |
| 104 | controller: function(){ |
| 105 | this.config = { |
| 106 | type: 'danger' |
Matteo Scandolo | a5d03d5 | 2016-07-21 11:35:46 -0700 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | }) |
| 110 | })(); |