Field Configuration Object
You pass an array of field configuration objects to the formly-form directive.
api-check
These are validated using api-check which will ensure that you're assigning the right properties and the right types. Aside from being required to specify the correct types, you are also required to set at least a
type
,template
, ortemplateUrl
. These attributes are mutually exclusive. All other properties are optional. Finally, you can only specify the properties listed below. Additional properties will result in an error. If you need custom properties, usetemplateOptions
ordata
.
Below are descriptions of the different properties of the field configuration object.
type (string)
The type
of field to be rendered. This is the recommended method for defining fields. Types must be pre-defined using formlyConfig.
template (string || function)
Can be set instead of type
or templateUrl
to use a custom html template form field. Recommended to be used with one-liners mostly (like a directive), or if you're using webpack with the ability to require templates :-)
If a function is passed, it is invoked with the field configuration object and can return either a string for the template or a promise that resolves to a string.
Add Normal HTML
This can be used to add HTML instead of a form field.
vm.fields = [
{
noFormControl: true,
template: '<p>Some text here</p>'
},
// templateUrl works too
{
noFormControl: true,
templateUrl: 'path/to/template.html'
}
];
See below for an explanation on why noFormControl
is needed.
templateUrl (string || function)
Can be set instead of type
or template
to use a custom html template form field. Works just like a directive templateUrl
and uses the $templateCache
If a function is passed, it is invoked with the field configuration object and can return either a string for the templateUrl or a promise that resolves to a string.
key (string)
By default form models are keyed by location in the form array, you can override this by specifying a key
.
vm.model = {};
vm.fields = [
{
// this field's ng-model will be bound to vm.model.username
key: 'username',
type: 'input'
}
];
defaultValue (any)
Use defaultValue
to initialize it the model. If this is provided and the value of the model at compile-time is undefined, then the value of the model will be assigned to defaultValue
.
Use the model
The
<formly-form>
represents the current state of the model and that's it. So if you want to default values in the form, simply initialize your model with that state and you're good. However, there are some use cases where it is much easier to use the form config to initialize the model, which is why this property was added.
hide (boolean)
Whether to hide the field. Defaults to false. If you wish this to be conditional, use hideExpression
. See below.
Uses ng-if
Unless otherwise specified by the
hide-directive
attribute on the formly-form or the preconfiguration in[formlyConfig](doc:formlyconfig)
hideExpression (string || function)
This is similar to expressionProperties
with a slight difference. You should (hopefully) never notice the difference with the most common use case. This is available due to limitations with expressionProperties
and ng-if
not working together very nicely.
model (object || string)
By default, the model
passed to the formly-field
directive is the same as the model
passed to the formly-form
. However, if the field has a model
specified, then it is used for that field (and that field only). In addition, a deep watch is added to the formly-field
directive's scope to run the expressionProperties
when the specified model
changes.
Note, the formly-form
directive will allow you to specify a string which is an (almost) formly expression which allows you to define the model as relative to the scope of the form.
expressionProperties (object)
An object where the key is a property to be set on the main field config and the value is an expression used to assign that property. The value is a formly expressions. The returned value is wrapped in $q.when
so you can return a promise from your function :-)
For example:
vm.fields = [
{
key: 'myThing',
type: 'someType',
expressionProperties: {
'templateOptions.label': '$viewValue', // this would make the label change to what the user has typed
// this would set that property on data to be whether or not model.myThing.length > 5
'data.someproperty.somethingdeeper.whateveryouwant': 'model.myThing.length > 5'
}
}
];
className (string)
You can specify your own class that will be applied to the formly-field
directive (or ng-form
of a fieldGroup
).
id (string)
This allows you to specify the id
of your field (which will be used for its name
as well unless a name
is provided). Note, you can also override the id
generation code using the formlyConfig extra
called getFieldId
.
Avoid this
If you don't have to do this, don't. Specifying IDs makes it harder to re-use things and it's just extra work. Part of the beauty that angular-formly provides is the fact that you don't need to concern yourself with making sure that this is unique.
name (string)
If you wish to, you can specify a specific name
for your ng-model
. This is useful if you're posting the form to a server using techniques of yester-year.
Avoid this
If you don't have to do this, don't. It's just extra work. Part of the beauty that angular-formly provides is the fact that you don't need to concern yourself with stuff like this.
data (object)
This is reserved for the developer. You have our guarantee to be able to use this and not worry about future versions of formly overriding your usage and preventing you from upgrading :-)
templateOptions (object)
This is reserved for the templates. Any template-specific options go in here. Look at your specific template implementation to know the options required for this.
Common Properties
angular-formly ships with the ngModelAttrsTemplateManipulator. This will automatically add properties like
required
, andplaceholder
to theng-model
element in your field.
templateManipulator (object of arrays of functions)
Allows you to specify custom template manipulators for this specific field. (use defaultOptions
in a type configuration if you want it to apply to all fields of a certain type).
vm.fields = [
{
template: 'foo',
templateManipulators: {
preWrapper: [
function(template, options, scope) {
// this is called for this field only before wrappers and before other manipulators
return template;
}
],
postWrapper: [
function(template, options, scope) {
// this is called for this field only after wrappers and before other manipulators
return template;
}
]
}
}
];
wrapper (string || array of strings)
This makes reference to setWrapper
in formlyConfig. It is expected to be the name of the wrapper. If given an array, the formly field template will be wrapped by the first wrapper, then the second, then the third, etc. You can also specify these as part of a type
(which is the recommended approach). Specifying this property will override the wrappers for the type for this field.
ngModelAttrs (object)
This is used by ngModelAttrsTemplateManipulator to automatically add attributes to the ng-model element of field templates. You will likely not use this often. This object is a little complex, but extremely powerful. It's best to explain this api via an example. For more information, see the guide on ngModelAttrs.
controller (controller name as string || controller function)
This is a great way to add custom behavior to a specific field. It is injectable with the $scope of the field, and anything else you have in your injector.
Injectable function
This is invoked using the
$injector
. If you minify your code (as you should) then you should make sure that this function is annotated correctly so dependency injection works properly. See the angular documentation on di for more information.
link (link function)
This allows you to specify a link function. It is invoked after your template has finished compiling. You are passed the normal arguments for a normal link function.
optionsTypes (string || array of strings)
Allows you to specify extra types to get options from. Duplicate options are overridden in later priority (index 1
will override index 0
properties). Also, these are applied after the type
's defaultOptions
and hence will override any duplicates of those properties as well.
modelOptions
Allows you to take advantage of ng-model-options
directive. Formly's built-in templateManipulator (see below) will add this attribute to your ng-model
element automatically if this property exists. Note, if you use the getter/setter
option, formly's templateManipulator will change the value of ng-model
to options.value
which is a getterSetter that formly adds to field options. For more information on ng-model-options, see this ng-conf talk and these egghead lessons.
noFormControl (boolean)
Used to tell angular-formly to not attempt to add the formControl
property to your object. This is useful for things like validation, but not necessary if your "field" doesn't use ng-model
(if it's just a horizontal line for example). Defaults to undefined.
watcher (object|array of watches)
An object which has at least two properties called expression
and listener
. The watch.expression
is added to the formly-form
directive's scope (to allow it to run even when hide
is true). You can specify a type ($watchCollection
or $watchGroup
) via the watcher.type
property (defaults to $watch
) and whether you want it to be a deep watch via the watcher.deep
property (defaults to false
).
Not a normal watch/formly expression
This differs slightly from a normal
$watch
function in very useful ways. See Formly Expressions for more information.
validators (object)
An object where the keys are the name of the validator and the values are Formly Expressions
Async validation
All function validators can return true/false/Promise. A validator passes if it returns true or a promise that is resolved. A validator fails if it returns false or a promise that is rejected.
Angular < 1.3
Formly defaults to use the $validators
api, which is only available in angular 1.3. If you are using 1.2, then the $parsers
api is used which doesn't support async validation out of the box. However, formly will keep track of the validations for you and ensure that the most recently resolved/rejected promise is what takes priority. Also, while the validation is in flight, formly emulates the $pending
api of 1.3 for your use in 1.2 as well, so you can safely use this and upgrade to 1.3 without worrying about the upgrade path for this api. You're welcome :-)
Validator as an object
You can alternatively specify a validator as an object with an
expression
and amessage
. This will unify how templates reference messages for when the validator has failed. Also, this should be used only for one-off messages (useng-messages-include
for generic messages).message
in this case should be an expression that is evaluated in exactly the same way a validator is evaluated. Theformly-custom-validation
directive will then add an object to the field options calledvalidation.messages
which is a map of functions where the key is the validation name and the value is a to function which returns the evaluated message. This is especially useful when usingng-messages
.
validation (object)
An object with a few useful properties mostly handy when used in combination with ng-messages
validation.messages
A map of Formly Expressions mapped to message names. This is really useful when you're using ng-messages
like in this example.
validation.show
A boolean you as the developer can set to specify to force options.validation.errorExistsAndShouldBeVisible
to be set to true when there are $errors
. This is useful when you're trying to call the user's attention to some fields for some reason. See this example
validation.errorExistsAndShouldBeVisible
This is set by angular-formly. This is a boolean indicating whether an error message should be shown. Because you generally only want to show error messages when the user has interacted with a specific field, this value is set to true based on this rule: field invalid && (field touched || validation.show)
(with slight difference for pre-angular 1.3 because it doesn't have touched support).
showError - shortcut
Because
options.validation.errorExistsAndShouldBeVisible
is really long, template authors can useshowError
instead which is simply a shortcut for this property.
Added Properties
The properties listed below are added to your field configuration.
formControl (NgModelController)
This is the NgModelController for the field. It provides you with awesome stuff like $errors
:-)
fc - shortcut
For template authors, a shortcut is provided on the
$scope
for this property calledfc
initialValue (any)
Used in combination with resetModel
and updateInitialValue
. Basically, this is set to the value this field represents at compile time, or the last time updateInitialValue
is called.
resetModel (function)
Will reset the field's model and the field control to the last initialValue
. This is used by the formly-form's options.resetModel
function.
updateInitialValue (function)
Will reset the field's initialValue
to the current state of the model. Useful if you load the model asynchronously. Invoke this when the model gets set. This is used by the formly-form's options.updateInitialValue
function.
value (getter/setter function)
This is a getter/setter function for the value that your field is representing. Useful when using getterSetter: true
in the modelOptions
(in fact, if you don't disable the ngModelAttrsTemplateManipulator
that comes built-in with formly, it will automagically change your field's ng-model
attribute to use options.value
.
runExpressions (function)
It is not likely that you'll ever want to invoke this function. It simply runs the expressionProperties
expressions. It is used internally and you shouldn't have to use it, but you can if you want to, and any breaking changes to the way it works will result in a major version change, so you can rely on its api.
Updated less than a minute ago