All Projects → simplabs → ember-validated-form-buffer

simplabs / ember-validated-form-buffer

Licence: MIT license
A validated form buffer that wraps Ember Data models for use in forms.

Programming Languages

javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to ember-validated-form-buffer

ember-formly
JavaScript powered forms for Ember
Stars: ✭ 24 (-47.83%)
Mutual labels:  ember, forms
ember-validity-modifier
Ember Octane addon to add custom validity (form validation) to form fields
Stars: ✭ 28 (-39.13%)
Mutual labels:  ember, forms
ember-do-forms
ember-do-forms handles the icky parts of forms that you don't want to, and leaves the rest to you.
Stars: ✭ 18 (-60.87%)
Mutual labels:  ember, forms
ember-foxy-forms
Ember Addon for Making Foxy Forms
Stars: ✭ 27 (-41.3%)
Mutual labels:  ember, forms
Ember Form For
Stars: ✭ 136 (+195.65%)
Mutual labels:  ember, forms
ember-right-click-menu
An easy and flexible addon to add context menus anywhere in your application
Stars: ✭ 14 (-69.57%)
Mutual labels:  ember
ember-audio
An Ember addon that makes working with the Web Audio API super EZ.
Stars: ✭ 33 (-28.26%)
Mutual labels:  ember
ember-pipeline
Railway oriented programming in Ember
Stars: ✭ 17 (-63.04%)
Mutual labels:  ember
ember-template-inspector
An ember add-on which opens the template file in the code editor while inspecting an element.
Stars: ✭ 15 (-67.39%)
Mutual labels:  ember
WebServer
High-performance multi-threaded tcp network server in c++11
Stars: ✭ 58 (+26.09%)
Mutual labels:  buffer
ember-emojione
EmojiOne helper and components for your Ember App
Stars: ✭ 16 (-65.22%)
Mutual labels:  ember
NALib
General purpose C sourcecode collection
Stars: ✭ 16 (-65.22%)
Mutual labels:  buffer
Build-Former
This is a library for building forms dynamically in Android.
Stars: ✭ 20 (-56.52%)
Mutual labels:  forms
LC-switch
Superlight vanilla javascript plugin improving forms look and functionality
Stars: ✭ 31 (-32.61%)
Mutual labels:  forms
ember-cli-nouislider
{{range-slider}} component for ember-cli powered by noUiSlider
Stars: ✭ 43 (-6.52%)
Mutual labels:  ember
jspm-ember-playground
Basic Ember app which uses JSPM as a package manager
Stars: ✭ 15 (-67.39%)
Mutual labels:  ember
ember-vertical-timeline
A Vertical Timeline for Ember.js apps 🚀
Stars: ✭ 19 (-58.7%)
Mutual labels:  ember
vim-emberlayout
Open Ember files in a grid
Stars: ✭ 18 (-60.87%)
Mutual labels:  ember
insect
🛠 Highly customisable, minimalistic input x select field for React.
Stars: ✭ 33 (-28.26%)
Mutual labels:  forms
fire
An idiomatic micro-framework for building Ember.js compatible APIs with Go.
Stars: ✭ 56 (+21.74%)
Mutual labels:  ember

Build Status

ember-validated-form-buffer

ember-validated-form-buffer implements a validating buffer that wraps Ember Data models and can be used in forms to buffer user inputs before applying them to the underlying model. The buffer also handles mixing client side validation errors and errors returned from the API as well as functionality that detects which API errors may have become obsolete due to modifications to the respective properties.

ember-validated-form-buffer helps implementing common forms functionality:

  • preventing modification of models until the form is submitted
  • implementing cancel/reset functionality
  • filtering irrelevant errors

It leverages ember-buffered-proxy for the buffering functionality and ember-cp-validations for client side validations.

Installation

Install ember-validated-form-buffer with

ember install ember-validated-form-buffer

Example

In order to define a validated form buffer on a controller or component, import the formBufferProperty helper and define a property that wraps the model instance. Pass in the validations mixin as returned by ember-cp-validations. When the form is submitted, apply the buffered changes and save the model or discard them to reset all user input:

import Ember from 'ember';
import { validator, buildValidations } from 'ember-cp-validations';
import formBufferProperty from 'ember-validated-form-buffer';

const Validations = buildValidations({
  name: validator('presence', true)
});

export default Ember.Controller.extend({
  data: formBufferProperty('model', Validations),

  actions: {
    submit(e) {
      e.preventDefault();

      this.get('data').applyBufferedChanges();
      this.get('model').save();
    },

    reset() {
      this.get('data').discardBufferedChanges();
    }
  }
});

Then instead of binding form inputs to model properties directly, bind them to the buffer instead:

<form onsubmit={{action 'submit'}}>
  <label>Name</label>
  {{input value=data.name}}
  <button type="submit">Save</button>
  <button type="button" onclick={{action 'reset'}}>Reset</button>
</form>

If you're not using 2 way data bindings for the input but Data Down/Actions Up, make sure to update the buffer property instead of the model's when the respective action is called:

<form onsubmit={{action 'submit'}}>
  <label>Name</label>
  <input value="{{data.name}}" onkeydown={{action (mut data.name) value='currentTarget.value'}}/>
  <button type="submit">Save</button>
  <button type="button" onclick={{action 'reset'}}>Reset</button>
</form>

API

The buffer

The buffer has methods for applying and discarding changes as well as properties for accessing its current error state.

  • applyBufferedChanges applies the changes in the buffer to the underlying model.

  • discardBufferedChanges discards the buffered changes to that the buffer's state is reset to that of the underlying model.

  • apiErrors returns the errors as returned by the API when the model was last submitted.

  • clientErrors returns the client side validation errors as returned by ember-cp-validations.

  • displayErrors returns both the API errors as well as the client side validation errors. This does not include any API errors on properties that have been changed after the model was submitted as changing a property that was previously rejected by the API potentially renders the respective error invalid.

  • hasDisplayErrors returns whether the buffer currently has any errors to display which is the case when displayErrors is not empty.

For further info on the buffer's API, check the docs of ember-buffered-proxy and ember-cp-validations respectively.

The buffer can be imported and used directly:

import { Buffer } from 'ember-validated-form-buffer';

const Validations = buildValidations({
  name: validator('presence', true)
});

export default Ember.Controller.extend({
  data: computed('model', function() {
    let owner = Ember.getOwner(this);
    return Buffer.extend(Validations).create(owner.ownerInjection(), {
      content: this.get('model')
    });
  }),

It is generally easier to use the formBufferProperty macro to define a form buffer property though:

The formBufferProperty helper

The formBufferProperty macro takes the name of another property that returns the Ember Data model to wrap in the buffer as well as a list of mixins that will be applied to the buffer. These mixins usually include the validation mixin as created by ember-cp-validations's buildValidations method.

If any of the provided mixins define an unsetApiErrors method, that method will be called whenever any property is changed on the buffer. The method returns a property name or an array of property names for which all API errors will be excluded from the displayErrors until the model is submitted to the API again. That way it's possible to hide API errors on a property when a related property changes:

import formBufferProperty from 'ember-validated-form-buffer';

const Validations = buildValidations({
  name: validator('presence', true)
});

export default Ember.Controller.extend({
  data: formBufferProperty('model', Validations, {
    unsetApiErrors() {
      let changedKeys = Ember.A(Object.keys(this.get('buffer')));
      if (changedKeys.includes('date') || changedKeys.includes('time')) {
        return 'datetime'; // whenever the "date" or "time" attributes change, also hide errors on the virtual "datetime" property
      }
    }
  })

License

ember-validated-form-buffer is developed by and © simplabs GmbH and contributors. It is released under the MIT License.

ember-validated-form-buffer is not an official part of Ember.js and is not maintained by the Ember.js Core Team.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].