Ember Octane 如何清除表格错误?
Ember Octane How to Clear Form Errors?
这个问题与
相关
问:清除表单错误的正确方法是什么?我该怎么做?每次用户进入表单时,我都希望它 运行 。表单错误在 Controller JS 文件中生成。使用案例如下:
- 用户导航到表单
- 用户提供了错误的输入,导致错误被
显示
- 用户离开表单并执行其他操作
- 用户返回表单并重新显示现有错误(我没有
希望这发生)
在 Ember Classic 中,我可以使用以下代码片段清除组件 JS 文件中的表单错误:
从“@ember/array”导入{A};
...
init() {
this._super(... arguments);
this.set('errors', A([]));
},
但是,在 Ember Octane 中,我收到以下 ESLint 错误:
Don't use this._super
in ES classes
ember/no-ember-super-in-es-classes
我尝试将代码片段更改为:
import { A } from '@ember/array';
...
init() {
super(... arguments);
this.set('errors', A([]));
}
不幸的是,我收到以下错误:
super() is only valid inside a class constructor of a subclass. Maybe
a typo in the method name ('constructor') or not extending another
class?
代码
模板组件 HBS:
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<h3>Change Password</h3>
<form class="m-t" role="form" {{on "submit" this.changePassword}}>
{{#each @errors as |error|}}
<div class="error-alert">{{error.detail}}</div>
{{/each}}
<div class="form-group">
<Input @type="password" class="form-control" placeholder="Old Password" @value={{this.oldPassword}} required="true" />
</div>
<div class="form-group">
<Input @type="password" class="form-control" placeholder="New Password" @value={{this.newPassword}} required="true" />
</div>
<div class="form-group">
<Input @type="password" class="form-control" placeholder="Confirm Password" @value={{this.confirmPassword}} required="true" />
</div>
<div>
<button type="submit" class="btn btn-primary block full-width m-b">Submit</button>
</div>
</form>
</div>
</div>
HBS 模板:
<Clients::ChangePasswordForm @changePasswordModel={{this.model}} @changePassword={{action 'changePassword'}} @errors={{this.errors}} />
组件 JS:
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class ChangePasswordForm extends Component {
constructor() {
super(...arguments);
this.errors = []
}
@tracked oldPassword;
@tracked newPassword;
@tracked confirmPassword;
@tracked errors;
@action
changePassword(ev) {
ev.preventDefault();
this.args.changePassword({
oldPassword: this.oldPassword,
newPassword: this.newPassword,
confirmPassword: this.confirmPassword
});
}
}
控制器JS
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class ChangePassword extends Controller {
@service ajax;
@service session;
@action
changePassword(attrs) {
if(attrs.newPassword == attrs.oldPassword)
{
this.set('errors', [{
detail: "The old password and new password are the same. The password was not changed.",
status: 1003,
title: 'Change Password Failed'
}]);
}
else if(attrs.newPassword != attrs.confirmPassword)
{
this.set('errors', [{
detail: "The new password and confirm password must be the same value. The password was not changed.",
status: 1003,
title: 'Change Password Failed'
}]);
}
else
{
let token = this.get('session.data.authenticated.token');
this.ajax.request(this.store.adapterFor('application').get('host') + "/clients/change-password", {
method: 'POST',
data: JSON.stringify({
data: {
attributes: {
"old-password" : attrs.oldPassword,
"new-password" : attrs.newPassword,
"confirm-password" : attrs.confirmPassword
},
type: 'change-passwords'
}
}),
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json'
}
})
.then(() => {
this.transitionToRoute('clients.change-password-success');
})
.catch((ex) => {
this.set('errors', ex.payload.errors);
});
}
}
}
我发布了一个 Ember-Twiddle:
https://ember-twiddle.com/364eaf05a2e1072994b61f255032eb62?openFiles=templates.application%5C.hbs%2C
经典ember是
init() {
this._super(...arguments);
}
ember Octane 使用类构造函数
constructor() {
super(...arguments);
}
Ember.js Octane vs Classic Cheat Sheet
看看我做的这个考试:
example
我已经编辑了你的旋转文件,
我向控制器添加了一个 clearErrors 操作,
@action
clearErrors(){
this.set('errors',[]);
}
然后将其作为参数传递给组件,
<Clients::ChangePasswordForm @changePasswordModel={{this.model}} @changePassword={{action 'changePassword'}}
@clearErrors={{action 'clearErrors'}}
@errors={{this.errors}} />
然后在组件的每个初始化上我调用 clearErrors ,
constructor() {
super(...arguments);
this.args.clearErrors();
}
这个问题与
问:清除表单错误的正确方法是什么?我该怎么做?每次用户进入表单时,我都希望它 运行 。表单错误在 Controller JS 文件中生成。使用案例如下:
- 用户导航到表单
- 用户提供了错误的输入,导致错误被 显示
- 用户离开表单并执行其他操作
- 用户返回表单并重新显示现有错误(我没有 希望这发生)
在 Ember Classic 中,我可以使用以下代码片段清除组件 JS 文件中的表单错误:
从“@ember/array”导入{A};
...
init() {
this._super(... arguments);
this.set('errors', A([]));
},
但是,在 Ember Octane 中,我收到以下 ESLint 错误:
Don't use
this._super
in ES classes ember/no-ember-super-in-es-classes
我尝试将代码片段更改为:
import { A } from '@ember/array';
...
init() {
super(... arguments);
this.set('errors', A([]));
}
不幸的是,我收到以下错误:
super() is only valid inside a class constructor of a subclass. Maybe a typo in the method name ('constructor') or not extending another class?
代码
模板组件 HBS:
<div class="middle-box text-center loginscreen animated fadeInDown">
<div>
<h3>Change Password</h3>
<form class="m-t" role="form" {{on "submit" this.changePassword}}>
{{#each @errors as |error|}}
<div class="error-alert">{{error.detail}}</div>
{{/each}}
<div class="form-group">
<Input @type="password" class="form-control" placeholder="Old Password" @value={{this.oldPassword}} required="true" />
</div>
<div class="form-group">
<Input @type="password" class="form-control" placeholder="New Password" @value={{this.newPassword}} required="true" />
</div>
<div class="form-group">
<Input @type="password" class="form-control" placeholder="Confirm Password" @value={{this.confirmPassword}} required="true" />
</div>
<div>
<button type="submit" class="btn btn-primary block full-width m-b">Submit</button>
</div>
</form>
</div>
</div>
HBS 模板:
<Clients::ChangePasswordForm @changePasswordModel={{this.model}} @changePassword={{action 'changePassword'}} @errors={{this.errors}} />
组件 JS:
import Component from '@glimmer/component';
import { tracked } from '@glimmer/tracking';
import { action } from '@ember/object';
export default class ChangePasswordForm extends Component {
constructor() {
super(...arguments);
this.errors = []
}
@tracked oldPassword;
@tracked newPassword;
@tracked confirmPassword;
@tracked errors;
@action
changePassword(ev) {
ev.preventDefault();
this.args.changePassword({
oldPassword: this.oldPassword,
newPassword: this.newPassword,
confirmPassword: this.confirmPassword
});
}
}
控制器JS
import Controller from '@ember/controller';
import { inject as service } from '@ember/service';
import { action } from '@ember/object';
export default class ChangePassword extends Controller {
@service ajax;
@service session;
@action
changePassword(attrs) {
if(attrs.newPassword == attrs.oldPassword)
{
this.set('errors', [{
detail: "The old password and new password are the same. The password was not changed.",
status: 1003,
title: 'Change Password Failed'
}]);
}
else if(attrs.newPassword != attrs.confirmPassword)
{
this.set('errors', [{
detail: "The new password and confirm password must be the same value. The password was not changed.",
status: 1003,
title: 'Change Password Failed'
}]);
}
else
{
let token = this.get('session.data.authenticated.token');
this.ajax.request(this.store.adapterFor('application').get('host') + "/clients/change-password", {
method: 'POST',
data: JSON.stringify({
data: {
attributes: {
"old-password" : attrs.oldPassword,
"new-password" : attrs.newPassword,
"confirm-password" : attrs.confirmPassword
},
type: 'change-passwords'
}
}),
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/vnd.api+json',
'Accept': 'application/vnd.api+json'
}
})
.then(() => {
this.transitionToRoute('clients.change-password-success');
})
.catch((ex) => {
this.set('errors', ex.payload.errors);
});
}
}
}
我发布了一个 Ember-Twiddle:
https://ember-twiddle.com/364eaf05a2e1072994b61f255032eb62?openFiles=templates.application%5C.hbs%2C
经典ember是
init() {
this._super(...arguments);
}
ember Octane 使用类构造函数
constructor() {
super(...arguments);
}
Ember.js Octane vs Classic Cheat Sheet
看看我做的这个考试: example
我已经编辑了你的旋转文件, 我向控制器添加了一个 clearErrors 操作,
@action
clearErrors(){
this.set('errors',[]);
}
然后将其作为参数传递给组件,
<Clients::ChangePasswordForm @changePasswordModel={{this.model}} @changePassword={{action 'changePassword'}}
@clearErrors={{action 'clearErrors'}}
@errors={{this.errors}} />
然后在组件的每个初始化上我调用 clearErrors ,
constructor() {
super(...arguments);
this.args.clearErrors();
}