仅对选定字段设置 'didValidate' true
set 'didValidate' true only for selected fields
环境
- Ember版本:2.0
- Ember命令行版本:2.13.0
- Ember CP 验证版本: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('didValidate', true) ; 启用所有 三个验证输入 字段并显示甚至 confirmation_token 字段的验证错误。但我只需要为 current_password 和 new_email 显示验证错误消息。当调用 verify 操作时,反之亦然
一种方法是,属性 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});
}
});
这是唯一的方法吗?
一种方法是在组件上创建一个 didValidate 对象
didValidate: computed(() => ({})),
当你点击else中的changeEmail时你可以
['newEmail', 'currentPassword'].forEach(key => {
this.set(`didValidate.${key}`, true);
});
它为 didValidate
对象中的每个 属性 创建一个键并将其设置为 true
。
然后在模板中,您可以通过传递
来显示每个字段的错误
didValidate.newEmail or didValidate.currentPassword
环境
- Ember版本:2.0
- Ember命令行版本:2.13.0
- Ember CP 验证版本: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('didValidate', true) ; 启用所有 三个验证输入 字段并显示甚至 confirmation_token 字段的验证错误。但我只需要为 current_password 和 new_email 显示验证错误消息。当调用 verify 操作时,反之亦然
一种方法是,属性 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});
}
});
这是唯一的方法吗?
一种方法是在组件上创建一个 didValidate 对象
didValidate: computed(() => ({})),
当你点击else中的changeEmail时你可以
['newEmail', 'currentPassword'].forEach(key => {
this.set(`didValidate.${key}`, true);
});
它为 didValidate
对象中的每个 属性 创建一个键并将其设置为 true
。
然后在模板中,您可以通过传递
来显示每个字段的错误didValidate.newEmail or didValidate.currentPassword