1

我正在使用 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 })
  ]
};
4

1 回答 1

0

好的,通过github问题找到了帮助

基本上在控制器中,改变

MyValidationClass;

类似于

MyValidationClass = MyValidationClass;

没有那个,MyValidationClass被设置为undefined

于 2020-01-23T06:35:25.760 回答