Custom Templates
angular-formly types for CSS frameworks
You can create your own templates with formlyConfig
in your run
function (or formlyConfigProvider
in a config function). This is the recommended approach if you want to customize your templates at all.
Prebuilt Templates
While it is recommended to create your own templates for ultimate customization and flexibility, there are prebuilt templates you can use:
- Vanilla HTML
- Bootstrap
- LumX
- Ionic
- angular-material (WIP)
- Foundation: (WIP, owner needed)
Creating a Custom Template
Creating a custom template is quite easy, but also very flexible. It can be as simple as:
formlyConfig.setType({
name: 'input',
template: '<input ng-model="model[options.key]" />'
});
or as complex as
formlyConfig.setType({
name: 'ipAddress',
extends: 'input',
wrapper: 'someWrapper',
defaultOptions: {},
link: function(scope, el, attrs) {},
controller: function($scope, yourInjectableThing) {},
apiCheck: {}
// and more...
});
Then someone would use your type like so:
// in a controller
this.fields = [
{
type: 'ipAddress',
// other stuff
}
];
You have a lot of freedom when it comes to writing templates. You don't even need to use the model
which means that you can have fields that are just part of the look and feel of your form. Because of angular-formly's ngModelAttrsTemplateManipulator, you really only have to put the ng-model
element where you want it (if you want it at all) and formly will take care of most of the rest. If you want to have any extra properties, you have pretty much full reign over the options.templateOptions
object. Just be aware that angular-formly does make use of common parameters like required
or onClick
to automatically add attributes to ng-model
elements. See formlyConfig for more information.
formlyConfig.setType options
Option | Data Type(s) | Description |
---|---|---|
name | string | How you reference the type for a specific field. |
template | string | Inlined template for the type. Can only be specified if there is no templateUrl |
templateUrl | string | The URL for a template (uses the $templateCache ). Can only be specified if there is no template. |
defaultOptions | object (must be valid field config) | This will be merged with the options the user of the type provides (so they can override these options). |
wrapper | string || array of strings | The name of a wrapper that has been registered with angular-formly |
controller | controller name || controller function | Provides you the ability to add custom behavior to the type without having to make an entire directive (you can make a directive instead if you wish). |
link | function(scope, el, attrs) | Provides the ability to manipulate the DOM of the field |
apiCheck | object of functions | angular-formly uses apiCheck for validating options. You can enforce the API to your type this way. |
apiCheckInstance | Instance of apiCheck | Your own instance of apiCheck so you have the correct prefix/suffix/url/etc. |
apiCheckFunction | 'throw' or 'warn' | Defaults to 'warn' . If you specify 'throw' then an error will be thrown instead of 'warn' |
apiCheckOptions | object | The options to use when the check fails. Defaults to something sensible. |
validateOptions | function(options) | Note: It is recommended that you use apiCheck with apiCheckInstance rather than validateOptions .This function will be invoked with the options of the field after it has been merged with it's optionsDefaults andany types that its type extends . Feel free to log warnings to the console or throw errors here to help users use yourtypes correctly. Recommended: Use apiCheck.js as this is what formly uses and will already be available to you. |
Template's $scope
See this example for an example what's accessible on the template's scope for your custom types.
Here's what you have available on the $scope
of your templates:
Variable Name | Description |
---|---|
options | The data provided to configure the field. As a template author, if you need custom properties, you use the templateOptions property on this object. Note, there are a few things that get added to your field configuration (and hence, these options). One of these is "formControl" which can be used to display errors to the user (for example). |
to | Shortcut to options.templateOptions |
fc | Shortcut to options.formControl |
showError | Shortcut to options.validation.errorExistsAndShouldBeVisible |
id | The id of the field. You shouldn't have to use this. |
index | The index of the field the form is on (in ng-repeat) |
form | the form controller the field is in |
model | the model of the form (or the model specified by the field if it was specified). |
fields | all the fields for the form |
formState | The object passed as options.formState to the formly-form directive. Use this to share state between fields. |
Updated less than a minute ago