{"id":23449550,"url":"https://github.com/danielmahon/mongoose-forms","last_synced_at":"2025-04-13T18:51:26.782Z","repository":{"id":4064237,"uuid":"5168151","full_name":"danielmahon/mongoose-forms","owner":"danielmahon","description":"Form library for Mongoose using Handlebars and Node Validator","archived":false,"fork":false,"pushed_at":"2012-05-23T05:28:32.000Z","size":108,"stargazers_count":1,"open_issues_count":0,"forks_count":5,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-04-14T02:20:04.523Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://flightofthought.com","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danielmahon.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-07-24T16:34:46.000Z","updated_at":"2015-04-30T09:45:39.000Z","dependencies_parsed_at":"2022-08-31T19:41:20.727Z","dependency_job_id":null,"html_url":"https://github.com/danielmahon/mongoose-forms","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielmahon%2Fmongoose-forms","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielmahon%2Fmongoose-forms/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielmahon%2Fmongoose-forms/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danielmahon%2Fmongoose-forms/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danielmahon","download_url":"https://codeload.github.com/danielmahon/mongoose-forms/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248765982,"owners_count":21158296,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-23T23:19:48.665Z","updated_at":"2025-04-13T18:51:26.757Z","avatar_url":"https://github.com/danielmahon.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Mongoose Forms\n\nA form templating and validation library for Mongoose ODM using Handlebars templates and node validator\n\n## Installing\n\n    $ npm install mongoose-forms\n\n## Usage:\n\n### Create a Form from a Model - Simple\n\n```javascript\nvar Form    = require('mongoose-forms').Form;\nvar Model   = require('./lib/models/Model.js')\n\nvar form    = Form(Model); // Form fields will be generated from schema\n                           // with default values and type detection\n```\n\n### Render Using Handlebars\n\nRegister our helpers\n\n```javascript\nmongooseForms.bindHelpers(Handlebars, 'bootstrap'); // Using the bootstrap markup style\n```\n\nCall from inside template\n\n```html\n\u003cdiv class=\"container\"\u003e\n    {{{renderForm formObject}}}\n\u003c/div\u003e\n```\n\n### Save Model from Form (showing Express POST route) \n\n```javascript\nvar Bridge = require('mongoose-forms').Bridge;\n\napp.post('/site/create', function(req, res) {\n  \n  var form = SiteForm();\n\n  if(!form.isValid(req.body)) { ... Rerender form, automatically showing errors ... }\n\n  Bridge(new Site, form) // Bridge populates Model from Form\n    .getModel()\n    .save(function(err, site) { });\n});\n```\n\n### Populate Form from Model (showing Express GET route)\n\n```javascript\napp.get('/site/:id', function(req, res) {\n\n  Site.findById(req.params.id, function(err, site) {\n\n    var form = Bridge(site, new SiteForm).getForm(); // bridge populates Form from Model\n    \n    res.render('update', { form: form });\n  });\n});\n```\n\n### Create a Form from a Model - Advanced\n\n```javascript\nvar forms   = require('mongoose-forms');\nvar Site    = require('../models/Site.js')\n\nvar form = forms.Form(Site, {\n  renderOuter: true,          // render the form container\n  class: 'form-horizontal',   // give the form a class\n  legend: 'Site Name',        // render a legend (only if renderOuter: true)\n  maps: ['name', 'slug'],     // map only to these members of model\n  method: 'post',             // form method\n  fields: {\n    actions: {                // define custom fields that may or may not exist in your model\n      template: 'Actions',    // provide a template name\n      order: 100,             // order acts like a weight\n      buttons: [              // custom fields can take arbitrary data\n        {\n          type: 'submit',\n          label: 'Submit Form',\n          'class': 'btn-primary'\n        }\n      ]\n    },\n    name: { // We can define validation and return sanitized data (or return nothing to simply passthrough)\n      validate: function(value, check, sanitize) {\n        check(value, 'Must be at least 6 characters').len(6);\n        check(value, \"Can't be more than 15 characters\").len(6, 15);\n        return sanitize(value).xss().trim();\n      }\n    }\n  }\n});\n\n```\n\n### Simplify life with a builder (name it something like: forms/User.js)\n\n```javascript\nvar forms = require('mongoose-forms');\nvar User  = require('../models/User.js');\n\nmodule.exports = function() {\n  return forms.Form(User, {\n    method: 'post',\n    action: '/user/create',\n    maps: ['username', 'password'],\n    fields: {\n      password: {\n        template: 'Password',\n        validate: function(value, check) {\n          check(value, 'Minimum 6 characters and maximum 10').len(6, 10);\n        }\n      },\n      submit: {\n        template: 'Submit'\n      }\n    }\n  });\n}\n```\n\nThen include it in your program, and do stuff with it!\n\n```javascript\nvar Bridge      = require('mongoose-forms').Bridge;\n\nvar User        = require('./lib/models/User.js');\nvar UserForm    = require('./lib/forms/User.js');\n\n\nUser.find({ username: 'Foobar' }, function(err, user) {\n  renderSomeTemplate({\n    form: Bridge(user, new UserForm).getForm()\n  });\n});\n\n```\n\n## License\n\nCopyright (c) 2012 Josh Hundley \u0026lt;josh.hundley@gmail.com\u0026gt;\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielmahon%2Fmongoose-forms","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanielmahon%2Fmongoose-forms","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanielmahon%2Fmongoose-forms/lists"}