|
1 | 1 | # ember/template-require-form-method |
2 | 2 |
|
| 3 | +🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix). |
| 4 | + |
3 | 5 | <!-- end auto-generated rule header --> |
4 | 6 |
|
5 | | -Require form elements to have a method attribute. |
| 7 | +This rule requires all `<form>` elements to have `method` attribute with `POST`, `GET` or `DIALOG` value. |
6 | 8 |
|
7 | | -Form elements should explicitly specify the HTTP method they use. This improves code clarity and helps catch potential issues. |
| 9 | +By default `form` elements without `method` attribute are submitted as `GET` requests. |
| 10 | +In usual applications `submit` event listeners are attached to `form` elements and `event.preventDefault()` is called to avoid form submission. |
8 | 11 |
|
9 | | -## Examples |
| 12 | +However in case of failure to prevent default action, form submission as `GET` request can leak sensitive end-user information. |
10 | 13 |
|
11 | | -This rule **forbids** the following: |
| 14 | +Example uses of `GET` requests: |
12 | 15 |
|
13 | | -```gjs |
14 | | -<template> |
15 | | - <form></form> |
16 | | -</template> |
17 | | -``` |
| 16 | +- non-secure data |
| 17 | +- bookmarking the submission result |
| 18 | +- data search query strings |
18 | 19 |
|
19 | | -```gjs |
20 | | -<template> |
21 | | - <form method='DELETE'></form> |
22 | | -</template> |
23 | | -``` |
| 20 | +**Caution** - this rules does not check for `formmethod` attribute on `form` elements themselves. |
24 | 21 |
|
25 | | -This rule **allows** the following: |
| 22 | +## Examples |
26 | 23 |
|
27 | | -```gjs |
28 | | -<template> |
29 | | - <form method='POST'></form> |
30 | | -</template> |
31 | | -``` |
| 24 | +This rule **forbids** the following: |
32 | 25 |
|
33 | 26 | ```gjs |
34 | 27 | <template> |
35 | | - <form method='GET'></form> |
| 28 | + <form>Hello world!</form> |
| 29 | + <form method=''></form> |
| 30 | + <form method='random'>Hello world!</form> |
36 | 31 | </template> |
37 | 32 | ``` |
38 | 33 |
|
39 | | -```gjs |
40 | | -<template> |
41 | | - <form method='DIALOG'></form> |
42 | | -</template> |
43 | | -``` |
| 34 | +This rule **allows** the following: |
44 | 35 |
|
45 | 36 | ```gjs |
46 | 37 | <template> |
47 | | - <form method='{{dynamicMethod}}'></form> |
| 38 | + <form method='post'>Hello world!</form> |
| 39 | + <form method='get'>Hello world!</form> |
| 40 | + <form method='dialog'>Hello world!</form> |
48 | 41 | </template> |
49 | 42 | ``` |
50 | 43 |
|
51 | 44 | ## Configuration |
52 | 45 |
|
53 | | -- `allowedMethods` (default: `['POST', 'GET', 'DIALOG']`) - Array of allowed form method values |
54 | | - |
55 | | -```js |
56 | | -// .eslintrc.js |
57 | | -module.exports = { |
58 | | - rules: { |
59 | | - 'ember/template-require-form-method': [ |
60 | | - 'error', |
61 | | - { |
62 | | - allowedMethods: ['POST', 'GET'], |
63 | | - }, |
64 | | - ], |
65 | | - }, |
66 | | -}; |
67 | | -``` |
| 46 | +The following values are valid configuration: |
| 47 | + |
| 48 | +- boolean - `true` to enable / `false` to disable |
| 49 | +- object -- An object with the following keys: |
| 50 | + - `allowedMethods` -- An array of allowed form `method` attribute values, default: `['POST', 'GET', 'DIALOG']` |
68 | 51 |
|
69 | 52 | ## References |
70 | 53 |
|
71 | | -- [HTML Spec - Form Method Attribute](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method) |
| 54 | +- [MDN - form method attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/form#attr-method) |
| 55 | +- [HTML spec - form method attribute](https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#attr-fs-method) |
0 commit comments