tellform/app/models/form_field.server.model.js

141 lines
2.3 KiB
JavaScript
Raw Normal View History

2015-06-29 22:51:29 +00:00
'use strict';
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
2015-09-18 16:32:17 +00:00
relationship = require('mongoose-relationship'),
2015-10-06 20:14:38 +00:00
mUtilities = require('mongoose-utilities'),
2015-09-18 16:32:17 +00:00
_ = require('lodash'),
2015-09-10 22:06:28 +00:00
Schema = mongoose.Schema;
2015-06-29 22:51:29 +00:00
2015-10-06 20:14:38 +00:00
var FieldOptionSchema = new Schema({
option_id: {
type: Number,
},
option_title: {
type: String,
},
option_value: {
type: String,
trim: true,
},
});
2015-06-29 22:51:29 +00:00
/**
2015-10-06 20:14:38 +00:00
* FormField Schema
2015-06-29 22:51:29 +00:00
*/
var FormFieldSchema = new Schema({
2015-09-18 16:32:17 +00:00
// formSubmission: {
// type: Schema.ObjectId,
// ref: 'FormSubmission',
// childPath: 'form_fields'
// },
title: {
2015-06-29 22:51:29 +00:00
type: String,
trim: true,
2015-10-06 20:14:38 +00:00
required: 'Field Title cannot be blank',
2015-06-29 22:51:29 +00:00
},
description: {
type: String,
default: '',
},
2015-09-03 18:21:56 +00:00
2015-09-18 16:32:17 +00:00
logicJump: {
type: Schema.Types.ObjectId,
ref: 'LogicJump'
},
2015-09-03 18:21:56 +00:00
2015-10-06 20:14:38 +00:00
fieldOptions: [FieldOptionSchema],
2015-06-29 22:51:29 +00:00
required: {
type: Boolean,
2015-06-30 11:05:44 +00:00
default: true,
2015-06-29 22:51:29 +00:00
},
disabled: {
type: Boolean,
default: false,
},
2015-07-27 18:11:43 +00:00
deletePreserved: {
type: Boolean,
default: false
},
2015-06-29 22:51:29 +00:00
fieldType: {
type: String,
2015-10-06 20:14:38 +00:00
required: true,
2015-06-29 22:51:29 +00:00
validate: [validateFormFieldType, 'Invalid field type']
},
fieldValue: Schema.Types.Mixed
2015-06-29 22:51:29 +00:00
});
2015-09-18 16:32:17 +00:00
// FormFieldSchema.plugin(relationship, { relationshipPathName:'formSubmission' });
2015-10-06 20:14:38 +00:00
FormFieldSchema.plugin(mUtilities.timestamp, {
createdPath: 'created',
modifiedPath: 'lastModified',
useVirtual: false
});
2015-08-18 21:44:36 +00:00
FormFieldSchema.static('validTypes', function(){
return [
'textfield',
'date',
'email',
'legal',
'url',
'textarea',
'statement',
'welcome',
'thankyou',
'file',
'dropdown',
'scale',
'rating',
'radio',
'checkbox',
'hidden',
'yes_no',
'natural',
'number'
];
});
// fieldType Validation
function validateFormFieldType(value) {
if (!value) { return false; }
var validTypes = [
'textfield',
'date',
'email',
'legal',
'url',
'textarea',
'statement',
'welcome',
'thankyou',
'file',
'dropdown',
'scale',
'rating',
'radio',
'checkbox',
'hidden',
'yes_no',
'natural',
'number'
];
if (validTypes.indexOf(value) > -1) {
return true;
}
return false;
2015-09-10 22:06:28 +00:00
};
2015-08-18 21:44:36 +00:00
2015-10-06 20:14:38 +00:00
// var cloneFieldSchema = _.cloneDeep(FormFieldSchema);
2015-09-18 16:32:17 +00:00
mongoose.model('Field', FormFieldSchema);
2015-10-06 20:14:38 +00:00
module.exports = FormFieldSchema;
2015-09-18 16:32:17 +00:00