EnForm
Preview
Props
| Name | Description | Default | Control |
|---|---|---|---|
| fields | An array of objects containing the entries for the form. Each object will map to the props of any 'EnInput*' component. array<Field> | unknown | complex object |
| horizontal | Sets the layout of the fields in the form by adding the 'form-horizontal' css class boolean | false | |
| gridFields | Lays the fields out as a grid row.
Note boolean | true | |
| submitText | Sets the readable text for the default submit button. string | "Submit" | |
| submitClass | Sets the CSS class for the default submit button. string | "btn-success" | |
| cancelText | Sets the readable text for the default cancel button. string | "Cancel" | |
| cancelClass | Sets the CSS class for the default cancel button. string | "btn-link" | |
| errorMessage | Sets the bottom error message when a form is submitted with validation errors. string | "Please fix all errors before submitting." | |
| labelClass | Sets the class used for the label column when using horizontal layout. string | "col-md-3" | |
| inputClass | Sets the class used for the input column when using horizontal layout. string | "col-md-9" | |
| showRequired | Displays asterisk (*) next to required field labels boolean | true | |
| renderSubmitButton | Whether or not to render the submit button responsible for the submit event for the form. boolean | true | |
| renderCancelButton | Whether or not to render the cancel button responsible for the cancel event for the form. boolean | false | |
| isSubmitting | Whether or not to render the 'loading spinner' icon indicating that the form is in the middle of a submit process. boolean | false | |
| preventMultiSubmit | Adds double-click protection to form submit button, in the form of preventing a second submit. Form will need to be re-rendered via a page reload or key change in order to reset. boolean | true | |
| formGroupClass | Sets class for each dynamically created form group. Use this to create additional columns of fields. string | "col-12" | |
| displayOnly | Sets all inputs in the fields json to display only. Also hides submit and cancel buttons. boolean | false | |
| emptyValue | What every display-only field shows in place of an empty value, unless a field sets its
own. string | boolean | false | |
| disableSubmit | Disables submit button and form submit event. boolean | false | |
| disableForm | Disables form and removes submit button. boolean | false | |
| validateOnMount | Validates form immediately on mount. boolean | false | |
| inModal | Sets CSS classes so that component can be used within a modal without needing to separate the buttons. boolean | false | |
| useTrackedJson | Uses internally tracked JSON values from the form for the returned data instead of serializing the form DOM. boolean | false | |
| showRequiredLegend | Shows the required legend at the top of the form when boolean | false |
Slots
form-top
Places content above the form
Variables
| Name | Type | Description |
|---|---|---|
errors | array | An list of error messages from invalid fields. |
meta | object | The current validation state of the form. |
fields
Slot to add all fields of the form. The parent container has the `flex-row` class which requires direct children to have `col*` classes. See examples for further explanation.
Variables
| Name | Type | Description |
|---|---|---|
fields | array.<Field> | A list of fields passed to the component via json. |
errors | array | An list of error messages from invalid fields. |
meta | object | The current validation state of the form. |
inputGroupProps | InputGroupProps | The form's own layout values, to pass through to groups here that should match the ones generated from |
<dynamic:setSlotName(field)>
No description
Variables
| Name | Type | Description |
|---|---|---|
name | unknown | — |
field | unknown | — |
buttons
Replaces default buttons.
Variables
| Name | Type | Description |
|---|---|---|
submitForm | function | Method that can be used to submit the form |
meta | object | The current validation state of the form. |
resetInitialState | function | Resets the form back to its initial values and resets validation |
additional-buttons
Places additional buttons after the default buttons.
Variables
| Name | Type | Description |
|---|---|---|
meta | object | The current validation state of the form. |
resetInitialState | function | Resets the form back to its initial values and resets validation |
form-bottom
Places content below the form and buttons.
Variables
| Name | Type | Description |
|---|---|---|
errors | array | An list of error messages from invalid fields. |
meta | object | The current validation state of the form. |
setSlotName(field)
Dynamic field created for each field passed in by json. Slot names are generated in `kebab-case`
Variables
| Name | Type | Description |
|---|---|---|
field | unknown | The field object from the list of fields |
Events
submit
Submit event.
Payload
| Name | Type | Description |
|---|---|---|
data | object | The serialized data from the form by name attribute of each field. |
resetFormState | function | Convenience method to reset the form state back to clean and allow a subsequent submission. |
validated
Emitted after the form validates, without form submission
Payload
| Name | Type | Description |
|---|---|---|
valid | boolean | If the form is valid or not |
errors | array | List of current field errors |
Methods
| Name | Description |
|---|---|
| hasError | Returns a boolean for if the form is currently showing invalid. |
| validate | Dynamically validate the form. |
| submitForm | Used to dynamically submit the form, usually from another component using a ref to this component. |
| resetFormState | Resets the validation state of the form, useful for creating a fresh set of validation flags. |
| resetInitialState | Resets the validation state of the form, useful for creating a fresh set of validation flags. |
| setErrors | Sets field error messages based on key value pairs of field name and array of string error messages. |
Examples
Using an external submit button
Exposing form state
The same gate works inside a modal: the buttons slot exposes the same meta, so :disabled="!meta.valid" on EnButtonSubmit keeps the confirm button disabled until the form is valid. The EnModal page shows the full arrangement, with the form's inModal prop placing the buttons in the modal footer.
Creating field layouts
The fields slot sits in a flex-row, so col-* classes on the groups lay them out as a grid. That row carries a negative side margin that only a col-* child cancels. For a form that lays its own rows out — inline form groups, a CSS grid, fields wrapped in sections — set :grid-fields="false" so the groups sit inside their container rather than 15px outside it. horizontal is not the way to avoid the row: it swaps in the horizontal form-group styles, which carry the same margin.
Horizontal form layout
Although we are trying to stay away from using this layout, there may be a need to still use it.
Easiest option is to pass down the labelClass and inputClass from the EnForm props using the slot variable within the fields slot. The below example just uses the preset defaults that EnForm has.