Skip to content

Commit

Permalink
Added includeEmptyFields option to allow blank submitted values to st…
Browse files Browse the repository at this point in the history
…ill be applied to req.body.
  • Loading branch information
Stuart Chinery committed Jul 18, 2014
1 parent 2020a1e commit 40db125
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ The following are the options that can be passed to Multer.
* `onFilesLimit()`
* `onFieldsLimit()`
* `onPartsLimit()`
* `includeEmptyFields`

Apart from these, Multer also supports more advanced [busboy options](https://github.com/mscdex/busboy#busboy-methods) like `highWaterMark`, `fileHwm`, and `defCharset`.

Expand Down Expand Up @@ -236,6 +237,16 @@ onPartsLimit: function () {
}
```

### includeEmptyFields

A Boolean value to specify whether empty submitted values should be applied to req.body.

Defaults to false;

```js
includeEmptyFields: true
```

## License (MIT)

Copyright (c) 2014 Hage Yaapa <[http://www.hacksparrow.com](http://www.hacksparrow.com)>
Expand Down
6 changes: 4 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ var qs = require('qs');
module.exports = function(options) {

options = options || {};
options.includeEmptyFields = options.includeEmptyFields || false;

// specify the destination directory, else, the uploads will be moved to the temporary dir of the system
var dest;
Expand Down Expand Up @@ -52,8 +53,9 @@ module.exports = function(options) {
// handle text field data
busboy.on('field', function(fieldname, val, valTruncated, keyTruncated) {

// don't attach to the body object, if there is no value
if (!val) return;
// if includeEmptyFields is false and there is no value then don't
// attach the fields to req.body
if (!options.includeEmptyFields && !val) return;

if (req.body.hasOwnProperty(fieldname) && val) {
if (Array.isArray(req.body[fieldname])) {
Expand Down

0 comments on commit 40db125

Please sign in to comment.