An Ember forms library that handles the icky parts of forms that you don't want to do, but makes no other assumptions otherwise.
Get started with:
ember install ember-do-forms
My experience with my ambitious application has proven that other (maintained) form builder addons either do too much, or are hard to customize. So I created ember-do-forms because it assumes very little about my form needs.
The basic idea for ember-do-forms is simple: assume as little as possible about my HTML and CSS structure, while providing some syntactic sugar for the icky parts.
For example, should forms validate my inputs? I'd argue that forms are just UX glue for your data, so forms shouldn't do that. Still I'd like validation UX (CSS, text, etc) to be handled by the form builder because it's a big chunk of boilerplate.
Another icky part is customisation, which most other form builder addons fail short on in varying degrees.
- Extremely easy to customize, thanks to contextual components.
- Bare minimum HTML with no CSS by default. Use it with any CSS framework you like.
- Binds error and success classes (if found) on a field
focusOutor just beforesubmit. Works withember-cp-validationsby default.ember-changeset-validationsworks too with a small configuration tweak. - Uses
ember-one-way-controlsunder the hood for controls. But easily extensible with any control type. - Fully compatible with
ember-test-selectors.
do-formis just a component that wraps componentsobjectis the object the form binds to. It's the only positional param.propertyNameis the property the field binds to on the object.labelTextis the text to display for the labelcontrolTypeis a one-way-control valid control. Just omit theone-way. For example{{field.do-control 'checkbox'}}will render a{{one-way-checkbox}}under the hood.submitpass in any action you want there. Or not, because it isn't required. Works great withember-concurrency!{{do-feedback}}shows the validation error message if it exists. It is bound by default tofield.errorMessage.{{do-hint}}shows any hint text you want via thehintTextparameter.- use any type of button you want. Use a bare
<button type='Submit'>, use it withember-async-buttonor do it with style.
You can also use the {{ember-do-forms/input-field}} component to combine all of the goodness of the {{do-field}} component (and its contextual components) into a single line:
If you want to skip labels and hints, just omit the label and hint arguments.
Also the classNames of its rendered sub-components can be modified using labelClasses, controlClasses, feedbackClasses and hintClasses.
Same idea as the {{ember-do-forms/input-field}} component but providing some syntactic sugar for checkboxes.
If you want to skip the hint, just omit the hint argument. The same classNames that can be applied to the input-field also apply to the checkbox-field.
You can customize the rendered CSS by modifying the default config. But you can also customize classes by individual component.
You can modify the contents of a field easily. Below there is a Bootstrap 4 custom checkbox for example. The default tagName for a field is <div>.
You can also modify the content of the {{do-control}} component. Use whatever you like, the context has access to id, value and validationClass, so your custom control will just work™. The default tagName is ''.
The {{do-feedback}} component can be easily customized as well. Just pass a block and it will have access to the error message, and displays only if there is one. The caveat here is that the component tag name should only be configured via wrapperTagName, and the reason is that this component should be hidden unless an error message is present. The default wrapperTagName is <div>.
The {{do-hint}} component is very easy to customize. Just pass a block and it will have access to hintText. The default tagName is <small>.
If you have added the excellent ember-test-selectors addon to your project you can freely use data-test-* to components that have tags and the ember-test-selectors will work as advertised. It doesn't work though for components that are wrapper components for other components, which ember-do-forms uses in many places (and other form builders use them as well).
You can learn more about why you should use ember-test-selectors by watching this video.
You can turn on the auto generation of data-test-* attributes by changing the default configuration:
module.exports = function(environment) {
var ENV = {
'ember-do-forms': {
autoDataTestSelectors: true
}
};
};Now, given you have something like:
You can use testSelector in your acceptance tests:
find(testSelector('do-field', 'lastName'));find(testSelector('do-label', 'lastName'));find(testSelector('do-control', 'lastName'));find(testSelector('do-feedback', 'lastName'));find(testSelector('do-hint', 'lastName'));
You can also manually set the attributes:
The caveat is that the names of the data-test-* attributes must match the component names for do-control, do-feedback and input-field components (which are tagless), and only those attributes are supported. For components with tags, like do-form, do-field, do-label and do-hint, these restrictions don't apply.
If you have more complex components for controls for example, rest assured, you can use them. Even some context gets passed in to your custom components!
The default configuration for ember-do-forms is very light.
module.exports = function(environment) {
var ENV = {
'ember-do-forms': {
// The path to be read on the object for an errors array
errorsPath: 'validations.attrs.{PROPERTY_NAME}.errors',
// Auto generate relevant data-test-* for components
// Overridable per component
autoDataTestSelectors: false,
// CSS classes to be applied to components
// Overridable per component
defaultClasses: {
form: [],
field: [],
label: [],
control: [],
feedback: [],
hint: []
},
// CSS classes to be applied to do-field and do-control
// components based on the validation state of the object
validationClasses: {
fieldSuccess: [],
fieldError: [],
controlSuccess: [],
controlError: []
}
}
};
};You can easily extend this configuration. For example Bootstrap 4 classes:
module.exports = function(environment) {
var ENV = {
'ember-do-forms': {
defaultClasses: {
form: [],
field: ['form-group'],
label: ['col-form-label'],
control: ['form-control'],
feedback: ['form-control-feedback'],
hint: ['form-text', 'text-muted'],
},
validationClasses: {
fieldSuccess: ['has-success'],
fieldError: ['has-danger'],
controlSuccess: ['form-control-success'],
controlError: ['form-control-danger']
}
}
};
};Any contribution, be it an issue, a feature or a bugfix is greatly appreciated ❤️
Also, if your feature or bugfix adheres to ember-do-forms's philosophy and is tested, I will give you commit rights.
I'm extremely thankful for the contributors of these projects as they've been a huge inspiration for me. From being an absolute beginner and not knowing what contextual components are, I have learned a lot by looking at the code and just experimenting and asking questions.