meteor无法验证用户配置文件架构

时间:2017-10-30 14:52:23

标签: javascript html meteor meteor-accounts meteor-autoform

我在meteor.js中创建了一个基于autoform和simpl-schema包的用户配置文件编辑页面。我还在架构中添加了Tracker

我想要求字段Profile.Username,但是当字段为空时它会进行验证。此字段也是唯一的,处理此属性非常有效。 此外,还需要Email字段,并且Tracker工作正常。

这是我的代码:

users.js

import { Mongo } from 'meteor/mongo';
import { Tracker } from 'meteor/tracker';
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);


Schema.User = new SimpleSchema({
    emails: {
        type: Array,
        optional: true
    },
    'emails.$': {
        type: Object,
        optional: true,     
    },
    'emails.$.address': {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    'emails.$.verified': {
        type: Boolean,
        optional: true,
        autoform: {
                type: 'boolean-checkbox'
            }
    },
    createdAt: {
        type: Date,
        autoValue: function() {
            return new Date();
        },
        autoform: {
            type: 'hidden'
        }
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true,
        autoform: {
            type: 'hidden'
        }
    }
}, {tracker: Tracker});

Meteor.users.attachSchema(Schema.User);

Meteor.users.allow({
      insert: function () { return true; },
      update: function () { return true; },
      remove: function () { return true; }
});

profile.js

import { Mongo } from 'meteor/mongo';
import { Tracker } from 'meteor/tracker';
import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

Schema = {};

Schema.UserProfile = new SimpleSchema({
    username: {
        type: String,
        label: 'Username',
        unique: true,
    },  
    firstName: {
        type: String,
        optional: true
    },
    lastName: {
        type: String,
        optional: true
    },
    birthday: {
        type: Date,
        optional: true,     
        autoform: {
            type: 'bootstrap-datepicker',
            datePickerOptions: {
                autoclose: true
            }
        }
    },
    gender: {
        type: String,
        optional: true,
        autoform: {
            type:'select-radio',
            options: function () {
                return [
                   { label: 'Male', value: 'Male'},
                   { label: 'Female', value: 'Female'},                
                   ];
            },
        }
    }
}, {tracker: Tracker});

和ProfileEdit.html

<template name="ProfileEdit">
        {{> Header}}
        {{> SideNav}}
        <div class="main-layout">
        <legend>Update Profile</legend>                        
        {{#if Template.subscriptionsReady}}
                {{# autoForm id="profileEdit" collection="Meteor.users"
                        doc=currentUser type="update"}}
                        {{> afObjectField name="emails.0"}}                      
                        {{> afObjectField name="profile"}}
                <div class="form-group">
                        <button type="submit" class="btn btn-primary">Submit</button>
                </div>
                {{/autoForm}}
        {{/if}}
        </div>
</template>

请帮帮我。

0 个答案:

没有答案