簡體   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