当前位置: 首页 > 软件库 > 程序开发 > >

ember-validated-form-buffer

授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发
软件类型 开源软件
地区 不详
投 递 者 郭琨
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

ember-validated-form-buffer

ember-validated-form-buffer implements a validating buffer that wraps EmberData models and can be used in forms to buffer user inputs before applying themto the underlying model. The buffer also handles mixing client sidevalidation errors and errors returned from the API as well as functionalitythat detects which API errors may have become obsolete due to modifications tothe 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 leveragesember-buffered-proxy forthe buffering functionality andember-cp-validations forclient 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, importthe formBufferProperty helper and define a property that wraps the modelinstance. Pass in the validations mixin as returned by ember-cp-validations.When the form is submitted, apply the buffered changes and save the model ordiscard 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 tothe 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 therespective 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 asproperties for accessing its current error state.

  • applyBufferedChanges applies the changes in the buffer to the underlyingmodel.

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

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

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

  • displayErrors returns both the API errors as well as the client sidevalidation errors. This does not include any API errors on properties thathave been changed after the model was submitted as changing a property thatwas previously rejected by the API potentially renders the respective errorinvalid.

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

For further info on the buffer's API, check the docs of ember-buffered-proxyandember-cp-validationsrespectively.

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 formbuffer property though:

The formBufferProperty helper

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

If any of the provided mixins define an unsetApiErrors method, that methodwill be called whenever any property is changed on the buffer. The methodreturns a property name or an array of property names for which all API errorswill be excluded from the displayErrors until the model is submitted to theAPI again. That way it's possible to hide API errors on a property when arelated 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 theMIT License.

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

 相关资料
  • Ember Form For This Ember.js addon will give you an easy way to build good forms: Supports all HTML5 input types, textarea and select, backed by ember-one-way-controls Automatically adds labels, hints

  • mars-validated springmvc springboot springcloud dubbo 参数校验 简单好用的 springmvc springboot springcloud dubbo 参数校验 此框架基于 spring 开发。 使用4步配置 1、导入jar <dependency> <groupId>com.github.fashion

  • 问题内容: 我想在执行如下方法之前使用注释来验证参数: 当我将此方法放在Spring应用程序的Controller中时,当Foo对象无效时,将执行并引发错误。但是,如果我在应用程序的Service层中的方法中放入相同的内容,则即使Foo对象无效,也不会执行验证并且该方法仅会运行。 你不能在服务层中使用注释吗?还是我必须做一些额外的配置才能使其正常工作? 更新: 我已经将以下两个bean添加到ser

  • 问题内容: 如果Service类使用Validated注释进行注释,则同一类无法自动装配自身。 这是在Spring Context尝试加载时引发的异常: 同样,当您有很多依赖于类的自身时,就会发生这种情况(当某个服务使用使用第一个服务的其他服务时)。我想知道@Validated注解,但是我总是在bean上遇到同样的错误。 有人知道我该怎么解决吗? 问题答案: 在这种情况下,注释与错误的自动装配无关

  • 1.7.0 新增 从 1.8.0 开始支持blur 时才触发校验以及 debounce,同 Validator 一样也开始支持异步校验。 表单,包含各种输入组件以及对应的校验;我们可以通过数据驱动的方式来生成完成表单。 示例 默认配置使用 一个完整的包含所有的内置表单相关组件。 <cube-form :model="model" :schema="schema" :immediate-

  • 此方法返回'NUMERIC FORM'的当前设置,该设置用于在系统上进行数学计算。 语法 (Syntax) FORM() 参数 (Parameters) 没有 返回值 (Return Value) 此方法返回'NUMERIC FORM'的当前设置,该设置用于在系统上进行数学计算。 例子 (Example) /* Main program */ say FORM() 当我们运行上述程序时,