compo-form
v0.10.25
Published
[![Build Status](https://travis-ci.org/atmajs/compo-form.png?branch=master)](https://travis-ci.org/atmajs/compo-form) [![Bower version](https://badge.fury.io/bo/compo-form.svg)](http://badge.fury.io/bo/compo-form)
Downloads
3
Readme
Form Component
with Twitter Bootstrap classes
a:form;
Input Elements
Ui
Attributes
action
Default is the current location. Endpoint to submit the form data to.Supports also dynamic entity key definition, e.g.:
a:form action='/user/:userId';
Now, when the model has the
userId
property, then the proper endpoint url is generated and the "edit"method
is used. Otherwise the model is submited to the/user
endpoint with thePOST
method.method
Default isPOST
. Http Method.method-edit
Default isPUT
.get
(String or just a flag
). If specified, the component will load the model from the endpoint with theGET
http method. Whenget
is just a flag, then theaction
value is used. Supports dynamic entity key definition.content-type
Default isapplication/json
. Accepts also:multipart/form-data
form-type
Default is''
. Accepts alsohorizontal
,inline
. Refer to the Bootstrap Formsoffset
. Default is0
. When the form type ishorizontal
this attribute defines thecol
size for labels.redirect
. Default is empty. When form successfully submits the data it will redict the page to the specified url.model-detached
(Default isfalse
) :muscle:The component creates its own model scope and set the to edit model to the
entity
property.This flag defines if the model should be shallow-copied before setting to the
entity
propertyin-memory
(Default isfalse
)Only signals are emitted without uploading to the endpoint
Mask interpolation are also supported.
// load the User model and display the form for it
a:form action='/user/~[userId]' method='PUT' get form-type=horizontal offset=4;
The example is similar to the dynamic entity key. But here we predefine the endpoint to be editable only.
Api
validate():string
Validate the model, and also all inner custom components (if any)To validate the custom components they must implement IValidation interface:
IValidation { // return error description or Error instance when validation fails validate():string|Error }
submit()
Collects form data from the model and inner custom componentsTo get custom components data, implement IFormBuilder interface:
IFormBuilder { // return json object, which is then merged with other data toJson():object }
Per default
a:form
sends json data. But ifmultipart/form-data
is set for the content-type, then Json is tranformed toFormData
instance. So you can upload also images and other binaries.setEntity(obj)
Set the new model and refresh the componentgetEntity()
Get current components modelloadEntity(url)
Loads the model from remote and apply it to the form. This function is automaticaly called on render start, whenget
attribute is defined.uploadEntity
PerformPOST/PATCH/PUT
action according toform
attributes. This function is called onsubmit
signal.notify(type, message)
Notifies about any status changes
Signals
a:form
componenent emits signals to children on various states
formActivity(type, ...args)
Types:
start
progress
: plus arguments'load|upload', percent
end
: variations('end', 'upload', json)
,('end', 'load', json)
complete
: plus argumentsjson
. When not in-memory flag is used then is equivalent to('end', 'upload', json)
error
: plus argumentsError
formGet
: plus argumentsObject
(server response)formPost
: plus argumentsObject
(server response)formPut
: plus argumentsObject
(server response)formPatch
: plus argumentsObject
(server response)formDelete
: plus argumentsObject
(server response)
formNotification(notification: Object<type, message>
Slots
submit
Submit entitydelete
Remove entity
Components
a:form
defines some nested components. Each component is placed in a template: ItemLayout
Input Elements
All editors have dualbind
component, sothat they are bound to the model with a two-way data model binding type.
Input
Attributes:
property
(required): Value in a model to edittype
(optional): HtmlInput type value: 'string/number/email/etc'placeholder
(optional): HtmlInput placeholderclass
(optional): Css class names
a:form {
Input property='some.foo';
}
Placeholders:
@label
(optional) Defines nodes for thelabel
in a.form-group
a:form { Input property='some.foo' { @label > b > 'I am label' } }
Text
textarea
Attributes:
property
(required): Value in a model to editplaceholder
(optional): HtmlInput placeholderrows
(optional): HtmlTextArearows
attributeclass
(optional): Css class names
a:form {
Text property='description';
}
Placeholders:
@label
(optional)
Checkbox
Attributes:
property
(required): Value in a model to edittext
(required): Input's label textclass
(optional): Css class names
a:form {
Checkbox property='baz' text='Should handle baz';
}
Placeholders:
@label
(optional) Defines nodes for thelabel
in a.form-group
a:form { Checkbox property='baz' text='Should handle baz' { @label > 'Lorem ipsum' } }
Radio
Attributes:
property
(required): Value in a model to editclass
(optional): Css class names
Placeholders:
@Option
(required) Defines eachOption
for the radio groupa:form { Radio property='letter' { @Option value='a' { // nodes 'Letter A' } @Option value='b' > 'Letter B' @Option value='c' > 'Letter C' } }
@label
(optional)
Select
Attributes:
property
(required): Value in a model to editclass
(optional): Css class names
Placeholders:
@Option
(required) Defines eachOption
for the radio groupa:form { Select property='letter' { @Option value='a' { // nodes 'Letter A' } @Option value='b' > 'Letter B' @Option value='c' > 'Letter C' } }
@label
(optional)
Image
Attributes:
property
(required): Value in a model to edit
a:form {
Image property='avatar';
}
Placeholders:
@label
(optional)
Array
Edit the arrays: edit items, add items, remove items.
Slots:
arrayItemAdd
arrayItemRemove
Attributes:
property
: Property of an array in a model to edit
Placeholders:
@template
is a template for each item@header
is a template to be rendered before the list@footer
is a template to be rendered after the list
Template
Wraps nested nodes in the ItemLayout.
Otherwise you can place any mask nodes inside the
a:form
component
Placeholders:
@template
(required)a:form { Template > @template { MyPicker > dualbind value='myvalue'; } }
Ui
a:form
has some default components to provide error/success/progress notifications.
Notification
See the implementation at Notification.mask
How to override
mask.define('a:form', `
let Notification {
.my-status {
h4 > '~[bind: $scope.notificationMsg ]'
}
}
`)
Progress
See the implementation at Progress.mask
Examples
# install atma toolkit
npm install atma -g
# run examples static server
npm run examples
# navigate `http://localhost:5777/examples/index.html?input`
Test
npm test
:copyright: MIT - Atma.js Project