Getting 'TypeError: user is null' when trying to validate form using ember-cp-validations

Getting 'TypeError: user is null' when trying to validate form using ember-cp-validations

我正在尝试在控制台中使用 ember-cp-validations and am following the basic steps in their video TypeError: user is null 添加表单验证。我是 ember 的新手,我确信它是我忽略的非常基本的东西。

注册表单组件

import Ember from 'ember';

export default Ember.Component.extend({
  user: null,

  actions: {
    save() {

      let user = this.get('user');

      user.validate()
        .then(({ validations }) => {

          if (validations.get('isValid')) {
            user.save();
          } else {
            alert("not valid");
          }

        });
    }
  }
});

用户模型

import DS from 'ember-data';
import { validator, buildValidations } from 'ember-cp-validations';

const Validations = buildValidations({
  firstname: validator('presence', true),

  email: [
    validator('presence', true),
    validator('format', { type: 'email' })
  ],

  username: validator('presence', true),

  password: [
    validator('presence', true),
    validator('length', {
      min: 4
    })
  ]
});

export default DS.Model.extend(Validations, {
  firstname:  DS.attr('string'),
  email:      DS.attr('string'),
  username:   DS.attr('stirng'),
  password:   DS.attr('string')
});

您需要像这样将新用户传递给组件:

//routes/users/new.js
...
model(){
  return this.get('store').createRecord('user');
}
...

然后在模板中放置注册组件:

//templates/users/new.hbs
{{register-form user=model}}