set' didValidate'仅对选定字段为true

时间:2017-12-17 05:04:29

标签: ember.js ember-data ember-cli ember-cp-validations

环境

  • Ember版本:2.0
  • Ember CLI版本:2.13.0
  • Ember CP Validations版本:3.4.0

重现步骤

HBS:

<div>
  <label> Email: <label>
  {{validated-input model=this placeholder="Enter new email" valuePath='new_email' id="new_email" didValidate=didValidate}}
  <label> Password: <label>
  {{validated-input model=this placeholder="Enter current password" valuePath='current_password' id="current_password" didValidate=didValidate}} 
  <button {{action "changeEmail"}}>Submit</button>
</div>

<div>
  <label> Confirmation Token: <label>
  {{validated-input model=this placeholder="Enter confirmation token" valuePath='confirmation_token' id="confirmation_token" didValidate=didValidate}}
  <button {{action "verify"}}>Verify</button>
</div>

JS:

import Ember from 'ember';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
  new_email: [
    validator('presence', true),
    validator('format', { type: 'email' })
  ],
  current_password: [
    validator('presence', true)
  ],
  confirmation_token: [
    validator('presence', true),
  ]
});

export default Ember.Component.extend(Validations, {
  changeEmail: function() {
    this.validate().then(() => {
      if (this.get('validations.attrs.new_email.isValid') && this.get('validations.attrs.current_password.isValid')) {
        ...
        ....
      } else {
        this.set('didValidate', true);
      }
  });
});

现在,当我点击提交时,会调用 changeEmail 操作,如果验证失败则设置 this.set(&#39; didValidate&#39;,true); 启用所有三个验证输入字段,并显示甚至 confirmation_token 字段的验证错误。但我需要仅针对 current_password new_email 显示验证错误消息。反之,当验证操作被调用时

2 个答案:

答案 0 :(得分:0)

一种方法是, didValidate

的唯一属性名称

例如:

<div>
  <label> Email: <label>
  {{validated-input model=this placeholder="Enter new email" valuePath='new_email' id="new_email" didValidate=didValidateEmail}}
  <label> Password: <label>
  {{validated-input model=this placeholder="Enter current password" valuePath='current_password' id="current_password" didValidate=didValidatePassword}} 
  <button {{action "changeEmail"}}>Submit</button>
</div>

<div>
  <label> Confirmation Token: <label>
  {{validated-input model=this placeholder="Enter confirmation token" valuePath='confirmation_token' id="confirmation_token" didValidate=didValidateToken}}
  <button {{action "verify"}}>Verify</button>
</div>

并在js中为每个字段手动设置属性为true或false:

changeEmail: function() {
  this.validate().then(() => {
    if (this.get('validations.attrs.new_email.isValid') && this.get('validations.attrs.current_password.isValid')) {
      ...
      ....
    } else {
      this.setProperties({didValidateEmail: true, didValidatePassword: true});
    }
});    

这是唯一的方法吗?

答案 1 :(得分:0)

执行此操作的一种方法是在组件

上创建didValidate对象
didValidate: computed(() => ({})),

当您在其他地方点击changeEmail时,您可以

['newEmail', 'currentPassword'].forEach(key => {
  this.set(`didValidate.${key}`, true);
});

didValidate对象中的每个属性创建一个键,并将其设置为true

然后在模板中,您可以通过传递

来显示每个字段的错误
didValidate.newEmail or didValidate.currentPassword