2017-06-29 44 views
1

我试图营造一种特定的简单模式的一个集合,我想,以确保:Aldeed Simple-Schema,如何在另一个字段中使用允许的值?

当用户在我selectsCollection进入一个新的选择,他会把期权价值为一体选定的值。

例如:

SelectsCollection.insert({name:"SelectOne",deviceType:"Select",options:["option1","option2","option3"],value:"option4",description:"This is the first select"}); 

这没有工作。我希望他只写3个选项中的一个。

这里我的架构:

SelectsCollection = new Mongo.Collection('Selects'); //Create a table 

SelectsSchema = new SimpleSchema({ 
    name:{  
     type: String, 
     label:"Name", 
     unique:true 
    }, 
    deviceType:{ 
     type: String, 
     allowedValues: ['Select'], 
     label:"Type of Device" 
    }, 
    options:{ 
     type: [String], 
     minCount:2, 
     maxcount:5, 
     label:"Select Values" 
    }, 
    value:{ 
     type: String, 
     //allowedValues:[options] a kind of syntax 
     // or allowedValues:function(){ // some instructions to retrieve the array of string of the option field ?} 
     label:"Selected Value" 
    }, 
    description:{ 
     type: String, 
     label:"Description" 
    }, 
    createdAt:{ 
     type: Date, 
     label:"Created At", 
     autoValue: function(){ 
      return new Date() 
     } 
    } 
}); 

SelectsCollection.attachSchema(SelectsSchema); 

任何想法? :)

非常感谢!

+0

我不认为这是可以只能用简单模式来完成。请详细说明为什么你不能简单地检查纯JS如果'价值'是在'选项'数组? if(options.indexOf(value)!== -1){// insert}'。 –

+0

我用'if(options.indexOf(value)!== -1){//插入}'作为Khang的一个自定义验证,你建议:) –

回答

0

这可能与现场的custom验证函数,这个函数里面,你可以从其他领域获取值来完成:

SelectsSchema = new SimpleSchema({ 
    // ... 
    options: { 
    type: [String], 
    minCount: 2, 
    maxcount: 5, 
    label: "Select Values" 
    }, 
    value: { 
    label: "Selected Value", 
    type: String, 
    optional: true, 
    custom() { 
     const options = this.field('options').value 
     const value = this.value 

     if (!value) { 
     return 'required' 
     } 

     if (options.indexOf(value) === -1) { 
     return 'notAllowed' 
     } 
    } 
    }, 
    // ... 
}); 

看看这里custom-field-validation了解更多信息

+0

谢谢!有用 !关于它的最后一个问题...什么意思:if(options.indexOf(value)** === -1 **)? –

+0

用于检查“选项”是否不包含“值” – Khang

相关问题