[英]How create a field in mongodb schema with pre-defined values, and make the user choose the value with a radio button
我正在创建一个架构以在管理控制台中创建事件,并且我具有以下架构:
{
name: String,
date: Date,
price: String
}
一切正常,我已经创建了CRUD,但是现在我需要在架构中添加一个Description字段。 而且此字段不是完全动态的,我需要它具有4个预定义的值:
Value 1: Blue
Value 2: Red
Value 3: Yellow
Value 4: Black
在我的表单上,我需要将用户选择的[单选按钮]绑定到描述值。
我有4个预定义的值,并且我有4个单选按钮的形式。 例:
Radio1 [If the user choose this, the value of description field will be blue]
Radio2 [If the user choose this, the value of description field will be red]
Radio3 [If the user choose this, the value of description field will be yellow]
Radio4 [If the user choose this, the value of description field will be the description black]
一个更大的例子:
如果用户使用以下值创建事件:
In the name input field, value = 'Super big event'
in the date input field, value = '20/03/2015' (in date format)
in the price input field, value = '50 dollars',
在说明单选按钮中,他选择值为1的单选按钮,输出为:
{
name: 'Super big event'
date: '20/03/2015' (in date format)
price: '50 dollars'
description: 'Blue'
}
我真的很感谢有人在这里帮我,我在Google上找不到任何可以帮助我的东西。
每个框架都有很多模式包,因此我建议在选择定制之前先进行检查。
您所遵循的一般模式为每个字段都涉及一个单独的对象。 因此,您的架构如下所示:
Schema = {
name: {type: String},
date: {type: Date},
description: {type: String, possibleValues: ['blue','red','yellow','black']}
}
然后,当您进行验证时,您将拥有一个寻找“可能的值”字段的函数。 如果存在,则确保提交的值是可能值的成员。
同样,无需自己构建它,但这是您要遵循的一般模式...
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.