ember-变更集验证未正确验证
ember-changeset-validations not validating properly
我正在使用 Ember 3.15 (Octane) 并尝试按照 github 上的示例让 ember-changeset-validations
正常工作,但很难对其进行验证。控制台中没有(代码相关的)错误,但 changeset.isValid
总是 returns true
。
{{! application/controller.js}}
import {action} from "@ember/object";
import MyValidationClass from '../Validations/myValidations';
export default class MyController extends Controller {
MyValidationClass;
@action
submit(changeset) {
changeset.save()
}
}
--
{{! application/template.hbs}}
<MyComponent
@changeset={{changeset this.model this.MyValidationClass}}
@onSubmit={{this.submit}}
/>
--
{{! application/components/mycomponent.hbs}}
<BsForm @formLayout="horizontal" {{on 'submit' (fn this.submit @changeset)}} @model={{@changeset}} as |form|>
<form.element
@controlType="text"
@label="Title"
@placeholder="Title"
@property="title"
@required={{true}}
/>
</BsForm>
--
{{! application/components/mycomponent.js}}
export default class MyComponent extends Component {
async submit(changeset) {
await changeset.validate();
if(changeset.isValid) // returns true even when the validation should fail
this.args.onSubmit(changeset);
}
}
--
{{! application/Validations/myValidations.js}}
import {
validateLength,
validatePresence
} from 'ember-changeset-validations/validators';
export default {
title: [
validatePresence(true),
validateLength({ min: 44 })
]
};
好的,通过 github issue
找到了帮助
基本上在controller里面,改
MyValidationClass;
类似于
MyValidationClass = MyValidationClass;
没有那个,MyValidationClass
被设置为 undefined
我正在使用 Ember 3.15 (Octane) 并尝试按照 github 上的示例让 ember-changeset-validations
正常工作,但很难对其进行验证。控制台中没有(代码相关的)错误,但 changeset.isValid
总是 returns true
。
{{! application/controller.js}}
import {action} from "@ember/object";
import MyValidationClass from '../Validations/myValidations';
export default class MyController extends Controller {
MyValidationClass;
@action
submit(changeset) {
changeset.save()
}
}
--
{{! application/template.hbs}}
<MyComponent
@changeset={{changeset this.model this.MyValidationClass}}
@onSubmit={{this.submit}}
/>
--
{{! application/components/mycomponent.hbs}}
<BsForm @formLayout="horizontal" {{on 'submit' (fn this.submit @changeset)}} @model={{@changeset}} as |form|>
<form.element
@controlType="text"
@label="Title"
@placeholder="Title"
@property="title"
@required={{true}}
/>
</BsForm>
--
{{! application/components/mycomponent.js}}
export default class MyComponent extends Component {
async submit(changeset) {
await changeset.validate();
if(changeset.isValid) // returns true even when the validation should fail
this.args.onSubmit(changeset);
}
}
--
{{! application/Validations/myValidations.js}}
import {
validateLength,
validatePresence
} from 'ember-changeset-validations/validators';
export default {
title: [
validatePresence(true),
validateLength({ min: 44 })
]
};
好的,通过 github issue
找到了帮助基本上在controller里面,改
MyValidationClass;
类似于
MyValidationClass = MyValidationClass;
没有那个,MyValidationClass
被设置为 undefined