简体   繁体   English

复选框与Stormpath + Jade + node.js + express

[英]Checkbox with Stormpath + Jade + node.js + express

I've been trying to expand the form from this tutorial ( https://stormpath.com/blog/build-nodejs-express-stormpath-app ) for my web app. 我一直在努力为我的网络应用程序扩展本教程( https://stormpath.com/blog/build-nodejs-express-stormpath-app )中的表单。 I want to have a checkbox and currently have the following code in my jade file: 我想要一个复选框,目前在我的jade文件中有以下代码:

div.form-group
      label.col-sm-4 Sports
      div.col-sm-8
        input.form-control(placeholder='e.g. What sports are you interested in?',
          name='sports',
          type='checkbox',
          value="true")

My .js file on the server side is as follows: 我在服务器端的.js文件如下:

var profileForm = forms.create({
  givenName: forms.fields.string({
    required: true
  }),
  surname: forms.fields.string({ required: true }),
  city: forms.fields.string(),
  state: forms.fields.string(),
  zip: forms.fields.string(),
  sports: forms.fields.string()
});

// A render function that will render our form and
// provide the values of the fields, as well
// as any situation-specific locals

function renderForm(req,res,locals){
  res.render('profile', extend({
    title: 'My Profile',
    csrfToken: req.csrfToken(),
    givenName: req.user.givenName,
    surname: req.user.surname,
    city: req.user.customData.city,
    state: req.user.customData.state,
    zip: req.user.customData.zip,
    sports: req.user.customData.sports
  },locals||{}));
}

// Export a function which will create the
// router and return it

module.exports = function profile(){

  var router = express.Router();

  router.use(cookieParser());

  router.use(bodyParser.urlencoded({ extended: true }));

  router.use(csurf({ cookie: true }));

  // Capture all requests, the form library will negotiate
  // between GET and POST requests

  router.all('/', function(req, res) {
    profileForm.handle(req,{
  success: function(form){
    // The form library calls this success method if the
    // form is being POSTED and does not have errors

    // The express-stormpath library will populate req.user,
    // all we have to do is set the properties that we care
    // about and then call save() on the user object:
    req.user.givenName = form.data.givenName;
    req.user.surname = form.data.surname;
    req.user.customData.city = form.data.city;
    req.user.customData.state = form.data.state;
    req.user.customData.zip = form.data.zip;
    req.user.customData.sports = form.data.sports;
    req.user.customData.save();
    req.user.save(function(err){
      if(err){
        if(err.developerMessage){
          console.error(err);
        }
        renderForm(req,res,{
          errors: [{
            error: err.userMessage ||
            err.message || String(err)
          }]
        });
      }else{
        renderForm(req,res,{
          saved:true
        });
      }
    });
  },

It appears as though the checkbox status is not saved because when I refresh it always returns to the same status. 看起来似乎没有保存复选框状态,因为当我刷新时它总是返回到相同的状态。 Does anyone know how I can create checkboxes that save state in the customData Stormpath allows you to save 10MB in? 有谁知道我如何创建在customData中保存状态的复选框Stormpath允许你节省10MB?

The issue appears to be with the markup for the input element, you'll need to tell it how to set the checked attribute for the element, based on the sports variable that you are passing down to the template: 问题似乎与输入元素的标记有关,您需要告诉它如何根据您传递给模板的sports变量设置元素的checked属性:

input.form-control(placeholder='e.g. What sports are you interested in?',
    name='sports',
    type='checkbox',
    value="true"
    checked=(sports ? "checked" : undefined))

I hope this helps! 我希望这有帮助! I work at Stormpath and I wrote the tutorial article that you've been looking at :) 我在Stormpath工作,我写了你一直在看的教程文章:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM