Created
November 1, 2014 20:51
-
-
Save firminochangani/1593175230c4dd172a30 to your computer and use it in GitHub Desktop.
[Keystone.js] Use admin forms outside admin #671
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Jade File | |
. | |
. | |
. | |
if exampleSubmitted | |
h1 Yeah, it works. | |
else | |
form(method='post') | |
input(type='hidden', name='action', value='example') | |
.form-group(class=validationErrors.name ? 'has-error' : null) | |
p Name | |
input(type='text', name='name', value=formData.name, placeholder="Insert your name") | |
.form-group(class=validationErrors.email ? 'has-error' : null) | |
p Age | |
input(type='number', name='age', value=formData.age, placeholder="Insert your age please") | |
.form-actions | |
button(type='submit') Send |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Model | |
var keystone = require('keystone'), | |
Types = keystone.Field.Types; | |
// Set new model | |
var Example = new keystone.List('Example', { | |
nocreate: true | |
}); | |
// Set the fields of your model | |
Example.add({ | |
name: { type: String, required: true, label: 'Age' }, | |
age: { type: Number, label: 'Age' }, | |
}); | |
Example.defaultColumns = 'name, age'; | |
// Regist your new model | |
Example.register(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Controller for view example.jade | |
var keystone = require('keystone'), | |
Example = keystone.list('Example'); | |
exports = module.exports = function(req, res) { | |
var view = new keystone.View(req, res), | |
locals = res.locals; | |
// Set locals | |
locals.formData = req.body || {}; | |
locals.validationErrors = {}; | |
locals.exampleSubmitted = false; | |
// On POST requests, add the Example item to the database | |
view.on('post', { action: 'example' }, function(next) { | |
var newExample = new Example.model(), | |
updater = newExample.getUpdateHandler(req); | |
updater.process(req.body, { | |
flashErrors: true, | |
fields: 'name, age', | |
errorMessage: 'Something went wrong' | |
}, function(err) { | |
if (err) { | |
locals.validationErrors = err.errors; | |
} else { | |
locals.exampleSubmitted = true; | |
} | |
next(); | |
}); | |
}); | |
view.render('example'); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Index file on root of routes | |
exports = module.exports = function(app) { | |
// Views | |
. | |
. | |
. | |
app.all('/example', routes.views.example); | |
. | |
. | |
. | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks @flowck ! And is it possible to call directly an admin UI component into a form of user side ? I'm wondering if a can configure user forms and integer the components.
Thanks