隐藏/禁用场来自req.body消失(前preSS JS bodyParser) [英] Hidden/Disabled fields disappear from req.body (Express JS bodyParser)

查看:83
本文介绍了隐藏/禁用场来自req.body消失(前preSS JS bodyParser)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直没能找到这个问题的答案。当使用的NodeJS,防爆preSS和防爆preSS Bodyparser - 和我意味着其余栈为此事 - 我碰到一个问题,既隐藏和禁用字段:他们不会在REQ现身提交表单时。体对象。

如果我理解正确的话,前preSS的bodyparser方面实际上是从另一个项目采取。无论如何,我还没有真正能够弄清楚为什么这些字段被删除或如何阻止这种情况发生。我怀疑它可能有一些做methodoverride,但启用/禁用这对有问题的领域没有任何影响。

所以..为自己和未来的Google员工:这里发生了什么?

下面是一些code,按要求:
玉:

 形式(行动=/管理/用户/编辑,方法=后,NG-提交='registerUser($事件),名称='形式',NOVALIDATE )
    .row(NG-秀=ifweareupdating)
        标签(=_ ID)_id:
        输入(类型=隐藏,ID =_ ID,名称=_ ID,NG-模型=newUser._id)
        输入(类型=文本,ID =_ notHiddenId',名字=_ notHiddenId,NG-模型=newUser._id)

EX $ P $干燥综合征:

  app.post(/管理/用户/编辑,pass.ensureAdmin,userRoutes.editUserPost);

userRoutes:

  exports.userRoutes.editUserPost =功能(REQ,资源,下一个){
    的console.log(req.body._id)//日志未定义
    的console.log(req.body._notHiddenId)//记录实际的id
}


解决方案

这已无关,与前preSS。只有的成功控制的是序列化,并当表单提交的浏览器发送。

的HTML规范定义是什么造就了成功的控制。


  

一个成功的控制是提交有效。每一个成功的
  控制有其当前值作为配对的一部分,其控制的名字
  提交的表单数据集。一个成功的控制必须定义
  一个FORM元素中,必须有一个控制的名称。


  
  

不过:<​​/ P>
  
  


      
  • 控件被禁用不能成功。

  •   
  • 如果表单包含多个提交按钮,只有激活的提交按钮是成功的。

  •   
  • 所有上复选框可能会成功。

  •   
  • 对于共享的名称属性的值相同的单选按钮,仅在开的单选按钮可能会成功。

  •   
  • 对于菜单,控件名称由SELECT元素提供,由OPTION元素提供的值。只有选择的选项可能
      成功的。当选择任何选项,该控制并非
      成功既不名称或任何值被提交给
      当表单提交服务器。

  •   
  • 选择的文件的当前值是一个或多个文件名称的列表。在提交的形式,每个文件的内容是
      提交的表单数据的其余部分。文件内容
      根据表格的内容类型包装的。

  •   
  • 的对象控制的当前值由对象的实现决定的。

  •   

  
  

如果表单提交时控制不具备的电流值,
  用户代理并不需要把它当作一个成功的控制。


  
  

此外,用户代理不应该考虑以下控件
  成功:


  
  

      
  • 重置按钮。

  •   
  • 对象元素的申报属性已设置。

  •   

  
  

隐藏管制和未因风格呈现
  表设置可能仍然是成功的。


您的问题实际上是棱角分明。 显然,它不设置隐藏输入。

I have not been able to find the answer to this question. When using NodeJS, Express, and the Express Bodyparser - and the rest of my MEAN stack for that matter - I run into an issue with both hidden and disabled fields: they do not show up in the req.body object when submitting a form.

If I understand correctly, the bodyparser aspect of express is actually taken from another project. Regardless, I have not really been able to figure out why these fields are being removed or how to stop this from happening. I suspect it might have something to do with methodoverride, but enabling/disabling this has no effect on the fields in question.

So.. for myself and future googler's: what is happening here?

Here's some code, as requested: JADE:

form(action="/admin/users/edit", method="post", ng-submit='registerUser($event)', name='form', novalidate)
    .row(ng-show="ifweareupdating")
        label(for="_id") _id:
        input(type="hidden", id="_id", name="_id", ng-model="newUser._id")
        input(type="text", id="_notHiddenId', name="_notHiddenId", ng-model="newUser._id")

EXPRESS:

app.post("/admin/users/edit", pass.ensureAdmin, userRoutes.editUserPost);

userRoutes:

exports.userRoutes.editUserPost = function(req,res,next) { 
    console.log(req.body._id) // logs undefined
    console.log(req.body._notHiddenId) // logs actual id
}

解决方案

This has nothing to do with Express. Only successful controls are serialized and sent by the browser when a form is submitted.

The HTML spec defines what makes a control successful.

A successful control is "valid" for submission. Every successful control has its control name paired with its current value as part of the submitted form data set. A successful control must be defined within a FORM element and must have a control name.

However:

  • Controls that are disabled cannot be successful.
  • If a form contains more than one submit button, only the activated submit button is successful.
  • All "on" checkboxes may be successful.
  • For radio buttons that share the same value of the name attribute, only the "on" radio button may be successful.
  • For menus, the control name is provided by a SELECT element and values are provided by OPTION elements. Only selected options may be successful. When no options are selected, the control is not successful and neither the name nor any values are submitted to the server when the form is submitted.
  • The current value of a file select is a list of one or more file names. Upon submission of the form, the contents of each file are submitted with the rest of the form data. The file contents are packaged according to the form's content type.
  • The current value of an object control is determined by the object's implementation.

If a control doesn't have a current value when the form is submitted, user agents are not required to treat it as a successful control.

Furthermore, user agents should not consider the following controls successful:

  • Reset buttons.
  • OBJECT elements whose declare attribute has been set.

Hidden controls and controls that are not rendered because of style sheet settings may still be successful.

Your problem is actually Angular. Apparently, it does not set the value of hidden inputs.

这篇关于隐藏/禁用场来自req.body消失(前preSS JS bodyParser)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆