Git Product home page Git Product logo

forms-mongoose's Introduction

Forms-mongoose allows auto-generation of forms from your Mongoose models

http://search.npmjs.org/#/forms-mongoose

Example

Mongoose

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Email = mongoose.SchemaTypes.Email;
var forms = require('forms-mongoose');

var AddressSchema = new Schema({
  category: {type: String, required: true, default: 'home', forms: {all:{}}},
  lines: {type: String, required: true, forms:{all:{widget:forms.widgets.textarea({rows:3})}}},
  city: {type: String, required: true}  
});

var PersonSchema = new Schema({
  email: { type: Email, unique: true, forms: {
    all: {
      type: 'email'
    }
  }},
  confirmed: { type: Boolean, required: true, default: false },
  name: {
    first: { type: String, required: true, forms: {
      new: {},
      edit: {}
    }},
    last: { type: String, required: true, forms: {
      new: {},
      edit: {}
    }}
  },
  address: [AddressSchema]
});

var PersonModel = mongoose.model('Person', PersonSchema);

Convert Mongoose Model to Forms Object

var forms = require('forms-mongoose');

var form = forms.create(PersonModel, 'new'); // Creates a new form for a "new" Person

// Use the form object as you would with Forms

console.log (form.toHTML());
// Note toHTML does not include the <form> tags, this is to allow flexibility.

//optionally create some static methods in the schema

PersonSchema.statics.createForm = function (extra) {
  return forms.create(this, extra);
}

PersonSchema.statics.createAddressForm = function (extra) {
  return forms.create(this.schema.paths.address, extra);
}

Requirements

Installation

npm install forms-mongoose

forms-mongoose's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

forms-mongoose's Issues

does this still work, as in examples?

I have a model like this:

var forms = require('forms-mongoose');
var mongoose = require('mongoose');
var schema = new mongoose.Schema({
    "created": {type: Date, default: Date.now, forms: {all:{}}},
    "name": {type:String, forms: {all:{}}},
    "verified": {type:Boolean, forms: {all:{}}},
    "token": {type:String, forms: {all:{}}},
    "email": {type:mongoose.SchemaTypes.Email, required:true, forms: {all:{}}},
    "website": {type:mongoose.SchemaTypes.Url},
    "admin": {type:Boolean, default: false, forms: {all:{}}}
});
schema.statics.createForm = function (extra) {
  return forms.create(this, extra);
}
module.exports = mongoose.model('User', schema);

when I run this:

form = forms.create(models.User, 'new');

// or

models.User.createForm('new')

I get this:

{ fields: 
   { '0': 'n',
     '1': 'e',
     '2': 'w',
     created: 
      { required: undefined,
        validators: [Object],
        widget: [Object],
        parse: [Function],
        bind: [Function],
        errorHTML: [Function],
        labelText: [Function],
        labelHTML: [Function],
        classes: [Function],
        toHTML: [Function],
        name: 'created' },
     name: 
      { required: undefined,
        validators: [Object],
        widget: [Object],
        parse: [Function],
        bind: [Function],
        errorHTML: [Function],
        labelText: [Function],
        labelHTML: [Function],
        classes: [Function],
        toHTML: [Function],
        name: 'name' },
     verified: 
      { required: undefined,
        validators: [Object],
        widget: [Object],
        parse: [Function],
        bind: [Function],
        errorHTML: [Function],
        labelText: [Function],
        labelHTML: [Function],
        classes: [Function],
        toHTML: [Function],
        name: 'verified' },
     token: 
      { required: undefined,
        validators: [Object],
        widget: [Object],
        parse: [Function],
        bind: [Function],
        errorHTML: [Function],
        labelText: [Function],
        labelHTML: [Function],
        classes: [Function],
        toHTML: [Function],
        name: 'token' },
     email: 
      { required: true,
        validators: [Object],
        widget: [Object],
        parse: [Function],
        bind: [Function],
        errorHTML: [Function],
        labelText: [Function],
        labelHTML: [Function],
        classes: [Function],
        toHTML: [Function],
        name: 'email' },
     admin: 
      { required: undefined,
        validators: [Object],
        widget: [Object],
        parse: [Function],
        bind: [Function],
        errorHTML: [Function],
        labelText: [Function],
        labelHTML: [Function],
        classes: [Function],
        toHTML: [Function],
        name: 'admin' } },
  bind: [Function],
  handle: [Function],
  toHTML: [Function] }

Note the 'n', 'e', 'w'...

Also, if I run form.toHMTL() I get this:

/Users/konsumer/Desktop/forms/node_modules/forms-mongoose/node_modules/forms/lib/forms.js:108
                return html + form.fields[k].toHTML(kname, iterator);
                                             ^
TypeError: Object n has no method 'toHTML'
    at /Users/konsumer/Desktop/forms/node_modules/forms-mongoose/node_modules/forms/lib/forms.js:108:46
    at Array.reduce (native)
    at Object.f.toHTML (/Users/konsumer/Desktop/forms/node_modules/forms-mongoose/node_modules/forms/lib/forms.js:106:45)
    at Object.<anonymous> (/Users/konsumer/Desktop/forms/app.js:7:19)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)

this seems to work:

var form = forms.create(models.User);
for (i in form.fields){
    if (form.fields[i].toHTML)
        console.log(form.fields[i].toHTML())
}

I get this:

<div class="field"><label for="id_created">Created</label><input type="text" name="created" id="id_created" /></div>
<div class="field"><label for="id_name">Name</label><input type="text" name="name" id="id_name" /></div>
<div class="field"><label for="id_verified">Verified</label><input type="checkbox" name="verified" id="id_verified" value="on" /></div>
<div class="field"><label for="id_token">Token</label><input type="text" name="token" id="id_token" /></div>
<div class="field required"><label for="id_email">Email</label><input type="email" name="email" id="id_email" /></div>
<div class="field"><label for="id_admin">Admin</label><input type="checkbox" name="admin" id="id_admin" value="on" /></div>

on closer inspection, this works, too:

var form = forms.create(models.User);
console.log(form.toHTML())

References

Maybe I am doing it wrong, but I can't seem to get references working.

var Project = mongoose.model('Project', new mongoose.Schema({
    name: { type:String, required:true, forms: {all:{}} },
    people: { type: [mongoose.Schema.Types.ObjectId], ref: 'Person', forms: {all:{widget:forms.widgets.multipleCheckbox}} },
    tasks: { type: [mongoose.Schema.Types.ObjectId], ref: 'Task', forms: {all:{widget:forms.widgets.multipleCheckbox}} },
    client: { type: mongoose.Schema.Types.ObjectId, ref: 'Client', forms: {all:{widget:forms.widgets.select}} },
    archived: { type: Boolean, default: false },
    _created: { type: Date, default: Date.now },
    _user: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }
}));

When I do a forms.create(Project), I get this:

<div class="field required">
<label for="id_name">Name</label>
<input type="text" name="name" id="id_name" />
</div>

'Boolean' type gives error

Using the latest version of Mongoose (3.1.2), the type 'Boolean' throws an error.

This can be fixed by adding this line to index.js before 'Date': 'string'

'Boolean': 'boolean',

Label Text only replaces first instance of Underscore

The functionality of this plugin is setup so underscores are replaced by spaces, however this only works on the first occurrence of an underscore die to the limitations of Javascript's replace method.

To fix, simply change line 53 in fields.js to:

label = name[0].toUpperCase() + name.substr(1).replace(/_/g, ' ');

P.S. If you want me to submit this and #2 as pull requests that's no problem, it's just the problems I've found are simply 1 line fixes

EDIT: Just realised that fields.js is in an external node_module and can't exactly be fixed by you. My bad

How to use it with Express?

Cant get it work with Express. it generates form but doesn't show error messages when I try to save form with errors.

Possible version issue?

Following your 2-line example:

var forms = require('forms-mongoose');

var form = forms.create(pcr, 'new'); // Creates a new form for a "new" pcr

// Use the form object as you would with Forms

console.log (form.toHTML());

I get these errors when I run the app. pcr is a mongoose Schema in my project:

/home/nmartin/Projects/pcr-appserver/node_modules/forms-mongoose/node_modules/forms/lib/forms.js:88
                return html + form.fields[k].toHTML(k, iterator);
                                             ^
TypeError: Object n has no method 'toHTML'

here are the versions I'm using:

[email protected] node_modules/forms-mongoose
├── [email protected]
└── [email protected] ([email protected])

type=url for mongoose.SchemaTypes.Url

Using mongoose-types we get Email & Url. It'd be cool is Url was type=url, and didn't make an error like this:

/Users/konsumer/Desktop/forms/node_modules/forms-mongoose/index.js:49
    throw new Error('Model does not have forms.type, probably on a virtual', p
          ^
Error: Model does not have forms.type, probably on a virtual
    at get_field (/Users/konsumer/Desktop/forms/node_modules/forms-mongoose/index.js:49:11)
    at Object.module.exports.create (/Users/konsumer/Desktop/forms/node_modules/forms-mongoose/index.js:109:17)
    at Object.<anonymous> (/Users/konsumer/Desktop/forms/app.js:6:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:902:3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.