繁体   English   中英

尝试使用ember-cp-validations验证表单时出现“ TypeError:用户为空”

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

我正在尝试使用ember-cp-validations添加表单验证,并按照其视频 TypeError: user is null的基本步骤进行操作TypeError: user is null控制台中TypeError: user is null 我是炭烬的新手,我确信它是我所忽略的非常基本的东西。

寄存器形式组件

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');
}
...

在模板中,您将register组件设置为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM