Components.Form Class
Render a form with the appropriate Bootstrap layout class (see formLayout
).
Allows setting a model
that nested Components.FormElement
s can access, and that can provide form validation (see below)
You can use whatever markup you like within the form:
{{#bs-form action="submit"}}
{{#bs-form-group validation=firstNameValidation}}
<label class="control-label">First name</label>
{{bs-input type="text" value=firstname}}
{{/bs-form-group}}
{{/bs-form}}
However to benefit from features such as automatic form markup, validations and validation markup, use Components.FormElement
as nested components. See below for an example.
Submitting the form
When the form is submitted (e.g. by clicking a submit button), the event will be intercepted and the default action will be sent to the controller. In case the form supports validation (see "Form validation" below), the "before" action is called (which allows you to do e.g. model data normalization), then the available validation rules are evaluated, and if those fail, the "invalid" action is sent instead of the default "action".
Use with Components.FormElement
When using Components.FormElement
s with their property
set to property names of the form's validation enabled
model
, you gain some additional powerful features:
- the appropriate Bootstrap markup for the given
formLayout
and the form element'scontrolType
is automatically generated - markup for validation states and error messages is generated based on the model's validation (if available), when submitting the form with an invalid validation, or when focusing out of invalid inputs
{{#bs-form formLayout="horizontal" model=this action="submit"}}
{{bs-form-element controlType="email" label="Email" placeholder="Email" property="email"}}
{{bs-form-element controlType="password" label="Password" placeholder="Password" property="password"}}
{{bs-form-element controlType="checkbox" label="Remember me" property="rememberMe"}}
{{bs-button defaultText="Submit" type="primary" buttonType="submit"}}
{{/bs-form}}
See the Components.FormElement API docs for further information.
Form validation
All version of ember-bootstrap beginning from 0.7.0 do not come with built-in support for validation engines anymore. Instead support is added usually by additional Ember addons, for example:
- ember-bootstrap-validations: adds support for ember-validations
- ember-bootstrap-cp-validations: adds support for ember-cp-validations
- ember-bootstrap-changeset-validations: adds support for ember-changeset
To add your own validation support, you have to:
- extend this component, setting
hasValidator
to true if validations are available (by means of a computed property for example), and implementing thevalidate
method - extend the Components.FormElement component and implement the
setupValidations
hook or simply override theerrors
property to add the validation error messages to be displayed
When validation fails, the appropriate Bootstrap markup is added automatically, i.e. the error classes are applied and the validation messages are shown for each form element. In case the validation library supports it, also warning messages are shown. See the Components.FormElement documentation for further details.
See the above mentioned addons for examples.
Item Index
Methods
Properties
Methods
submit
privateSubmit handler that will send the default action ("action") to the controller when submitting the form.
If there is a supplied model
that supports validation (hasValidator
) the model will be validated before, and
only if validation is successful the default action will be sent. Otherwise an "invalid" action will be sent, and
all the showValidation
property of all child Components.FormElement
s will be set to true, so error state and
messages will be shown automatically.
Properties
childFormElements Array
protectedAn array of Components.FormElement
s that are children of this form.
formLayout string
publicSet the layout of the form to either "vertical", "horizontal" or "inline". See http://getbootstrap.com/css/#forms-inline and http://getbootstrap.com/css/#forms-horizontal
hasValidator boolean
protectedCheck if validating the model is supported. This needs to be implemented by another addon.
horizontalLabelGridClass string
publicThe Bootstrap grid class for form labels. This is used by the Components.FormElement
class as a default for the
whole form.
Default: 'col-md-4'
layoutClass string
protectedBootstrap form class name (computed)
model Ember.Object
publicSet a model that this form should represent. This serves several purposes:
- child
Components.FormElement
s can access and bind to this model by theirproperty
- when the model supports validation by using the ember-validations mixin,
child
Components.FormElement
s will look at the validation information of theirproperty
and render their form group accordingly. Moreover the form'ssubmit
event handler will validate the model and deny submitting if the model is not validated successfully.
novalidate boolean
publicIf set to true novalidate attribute is present on form element
Default: null
submitOnEnter boolean
publicIf set to true pressing enter will submit the form, even if no submit button is present
Default: false
Events
action
public
A handler called when submit has been triggered and the model has passed all validations (if present).
Event Payload:
-
Object
Objectmodel The form's
model
-
Object
Objectresult The returned result from the validate method, if validation is available
before
public
A handler called before the form is validated (if possible) and submitted.
Event Payload:
-
Object
Objectmodel The form's
model
invalid
public
A handler called when validation of the model has failed.
Event Payload:
-
Object
Objecterror