2.2.8 • Published 5 years ago
angular-autoFields-bootstrap v2.2.8
angular-autofields-bootstrap
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="js/autofields.js"></script>
<!-- with bootstrap -->
<script type="text/javascript" src="js/autofields-bootstrap.js"></script>Usage
- If you're doing this manually and using bootstrap, be sure to install Angular-UI Bootstrap for date popover support
- Include the
autofields.jsscript provided by this component into your app. If you are using bootstrap, also includeautofields-bootstrap.js - add
autofieldsas 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
propertythe data property to bind totypethe type of field. Options include: checkbox, date, select, textarea, any text variation (ie. password, text, email, number)labelthe label for the field. If no label is provided it will convert the property name to title case. If you don't want a label, set it's value to ''placeholderthe placeholder for the field. If no placeholder is provided it will use the label. If you don't want a placeholder, set it's value to ''helpa block of help or description text to be displayed beneath the fieldattrany additional attributes you would like to have on the object. Camelcase is converted to dash notation. Validation properties can go here.listthe string that goes into ng-options for select fieldsrowsnumber of textarea rows (default: 3)columnsnumber of sm columns a field should span if the type is multiple. If this is applied at the same level as the multiple type, it will apply it to all of it's fields.msgsvalidation messages for corresponding validation properties on the fieldvalidateenable/disable validation for the field (default: true)addonsarray of addon objects to be included with the inputbuttonis a button (default: false)iconclass string for an icon to include, empty or null implies no iconcontentstring to be placed in the addonbeforeprepend the addon (default: false)
Options
Standard
classesobject with an array of classes for each element of a field group: container, input, labelattributesobject with default attribute-value pairs for each element of a field group: container, input, labeldisplayAttributesarray of attributes that affect field displaycontainerthe html for the div the will hold the fieldstextareaRowsthe default amount of rows for a textarea (3)fixUrlwhether or not url type fields should have http:// auto added (true)
With Validation
validationsettings for validationenabledenabled/disable validation (enabled by default)showMessagesenabled/disable validation messages (enabled by default) *defaultMsgsdefault validation messages when none is specified in the field schema
####With Bootstrap
classesadds 8 new element class arrays: row, col, colOffset, helpBlock, inputGroup, inputGroupAddon, inputGroupAddonButton, buttonlayoutlayout options for the fieldstypeform type:basic | horizontallabelSizehow many columns a label should span *inputSizehow many columns an input should spandefaultOptionthe text for the default select option (Select One)dateSettingssettings for the date fields (see angular-ui-bootstrap's date picker)datepickerOptionssettings for the date picker (see angular-ui-bootstrap's date picker)
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)
typesa string or array of strings with field types that will be used to map to the handlerhandlera function that will be called by AutoFields to create the fields html. AutoFields will pass directive, field, and field indexdirectivedirective properties, options, and elements:containerthe autofields container elementoptionsthe options for the directivefieldthe field schema currently being processedindexthe index of the field in the field schema array
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, '<input/>');
if(fieldElements.label) fieldElements.label.prepend(fieldElements.input);
return fieldElements.fieldContainer;
});
}]);Adding New Field Properties
$autofieldsProvider.registerMutator(key, mutator, options)
keysomething the mutator can be referenced by in require & override requestsmutatorcalled by autofields after the creation of a field elementdirectivecontainerthe autofields container elementoptionsthe options for the directivefieldthe field schema currently being processedfieldElementsan object containing:fieldContainerthe container element for the field's label and inputlabelthe label elementinputthe input elementvalidationwhether or not to include validation requires validation*msgsan array of possible error messages requires validation*validMsga field's valid message requires validation*
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('<p/>');
fieldElements.helpBlock.addClass(directive.options.classes.helpBlock.join(' '))
fieldElements.helpBlock.html(field.help);
fieldElements.fieldContainer.append(fieldElements.helpBlock);
return fieldElements;
});
}]);Notes
- It shares the scope of it's parent so that it can access the data on the scope
- To make it work on IE8, just add a polyfill for Array.isArray()