Skip to content

Commit 0179abf

Browse files
committed
doc: how to make filter error the whole form.parse
fixes node-formidable#941
1 parent e4f29e7 commit 0179abf

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ See it's defaults in [src/Formidable.js DEFAULT_OPTIONS](./src/Formidable.js)
343343
newFilename. Must return a string. Will be joined with options.uploadDir.
344344

345345
- `options.filter` **{function}** - default function that always returns true.
346-
Use it to filter files before they are uploaded. Must return a boolean.
346+
Use it to filter files before they are uploaded. Must return a boolean. Will not make the form.parse error
347347

348348
- `options.createDirsFromUploads` **{boolean}** - default false. If true, makes direct folder uploads possible. Use `<input type="file" name="folders" webkitdirectory directory multiple>` to create a form to upload folders. Has to be used with the options `options.uploadDir` and `options.filename` where `options.filename` has to return a string with the character `/` for folders to be created. The base will be `options.uploadDir`.
349349

@@ -371,7 +371,7 @@ form.bytesExpected;
371371

372372
#### `options.filter` **{function}** function ({name, originalFilename, mimetype}) -> boolean
373373

374-
**Note:** use an outside variable to cancel all uploads upon the first error
374+
Behaves like Array.filter: Returning false will simply ignore the file and go to the next.
375375

376376
```js
377377
const options = {
@@ -382,6 +382,25 @@ const options = {
382382
};
383383
```
384384

385+
**Note:** use an outside variable to cancel all uploads upon the first error
386+
387+
**Note:** use form.emit('error') to make form.parse error
388+
389+
```js
390+
let cancelUploads = false;// create variable at the same scope as form
391+
const options = {
392+
filter: function ({name, originalFilename, mimetype}) {
393+
// keep only images
394+
const valid = mimetype && mimetype.includes("image");
395+
if (!valid) {
396+
form.emit('error', new formidableErrors.default('invalid type', 0, 400)); // optional make form.parse error
397+
cancelUploads = true; //variable to make filter return false after the first problem
398+
}
399+
return valid && !cancelUploads;
400+
}
401+
};
402+
```
403+
385404

386405
### .parse(request, ?callback)
387406

0 commit comments

Comments
 (0)