我正在使用 Ember 3.15 (Octane) 并尝试ember-changeset-validations
按照他们 github 上的示例开始工作,但很难对其进行验证。控制台中没有(与代码相关的)错误,但changeset.isValid
总是返回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 })
]
};