Parcourir la source

Documented custom fields

Brendan Abolivier il y a 7 ans
Parent
révision
2de4abe94c
Signé par: Brendan Abolivier <contact@brendanabolivier.com> ID de la clé GPG: 8EF1500759F70623
1 fichiers modifiés avec 78 ajouts et 1 suppressions
  1. 78
    1
      README.md

+ 78
- 1
README.md Voir le fichier

@@ -80,7 +80,29 @@ First, you must rename the `settings.example.conf` into `settings.conf`, and edi
80 80
     ],
81 81
     "formOrigin": "https://example.tld",
82 82
     "language": "en",
83
-    "labels": true
83
+    "labels": true,
84
+	"customFields": {
85
+		"deadline": {
86
+			"label": "Development deadline",
87
+			"type": "select",
88
+			"options": [
89
+				"A week",
90
+				"A month",
91
+				"More than a month"
92
+			],
93
+			"required": true
94
+		},
95
+		"domain": {
96
+            "label": "Website domain",
97
+            "type": "text",
98
+			"required": false
99
+        },
100
+		"budget_max": {
101
+			"label": "Maximum budget (€)",
102
+			"type": "number",
103
+			"required": true
104
+		}
105
+	}
84 106
 }
85 107
 ```
86 108
 
@@ -94,10 +116,46 @@ The `language` string tells SMAM in which language you want your form served. Po
94 116
 
95 117
 Finally, the `labels` setting is a boolean to set whether or not labels will be displayed in the `<form></form>` block. If set to `false`, the form will still display the front-end strings ("Your name", "Your e-mail address"...), but only as placeholders in the text fields. If set to true, the said strings will appear as text fields' placeholders but also as labels outside of the fields. If not set, defaults to `true`.
96 118
 
119
+The `customFields` section is optional and describes custom form fields, which are described below.
120
+
121
+## Custom fields
122
+
123
+SMAM allows you to add custom fields to your form (in addition to the default ones, which are the sender's name, the sender's e-mail address, the message's subject and the message's content). These fields will be added in your form below the content's field, in a tag defined by the settings file (one of &lt;input&gt;, &lt;select&gt; and &lt;textarea&gt;). We'll see below how to set the field's type.
124
+
125
+A custom field is defined in the `customFields` section of your settings file, as described below:
126
+
127
+```json
128
+"field_name": {
129
+	"label": "My field",
130
+	"type": "select",
131
+	"required": true,
132
+	"options": [
133
+		"Option 1",
134
+		"Option 2",
135
+		"Option 3"
136
+	]
137
+}
138
+```
139
+
140
+* **field_name** (required) is an identifier for your field, it will only be used internally by the software.
141
+* **label** (required) is both the label/placeholder displayed with your field in the form and the label displayed next to the user-input value in the final e-mail.
142
+* **type** (required) is the type of your field. If you set it to "select", the form field will use a &lt;select&gt; tag (this will require the `options` parameters being set). "textarea" will set the field to use the &lt;textarea&gt; tag. If any other value is set here, it will be placed in the `type` attribute of an &lt;input&gt; tag. Head over [here](https://developer.mozilla.org/fr/docs/Web/HTML/Element/Input#attr-type) for a full list of accepted input types. Please not that the `checkbox` and `radio` types aren't currently supported (but will be in the future). There's no support planned for the `submit` and `reset` types.
143
+* **required** (optional) is a boolean. Set it to true and the field will be set as required in your form. A modern browser should prevent an user to send the form if a required field isn't filled. On top of that, SMAM's server will check the field marked as required to make sure they've been filled.
144
+* **options** (optional) is an array of possible values. This is currently useful only if your field is of the "select" type.
145
+
97 146
 ## Templating
98 147
 
99 148
 Each e-mail sent by the form follows a template described in `template.pug` (it's [Pug](pugjs.org/)). If you want to change the way the e-mails you receive are displayed in your mailbox, just edit it! You don't even need to restart the server aftewards :smile:
100 149
 
150
+The template also features custom fields, iterating over the `custom` object, containing the field's label and user-input value:
151
+
152
+```json
153
+"field_name": {
154
+	"label": "My field",
155
+	"value": "Hello"
156
+}
157
+```
158
+
101 159
 ## Personnalising
102 160
 
103 161
 As you might have already seen, the contact form is generated without any form of style except your browser's default one. But that doesn't meen that you have to add an ugly form to your site to receive contact e-mails, as every element has a specific id (beginning with the `form_` prefix), allowing you to use your own style on your contact form.
@@ -127,6 +185,25 @@ The generated form will look like this:
127 185
 </div>
128 186
 ```
129 187
 
188
+Custom fields will be formatted the same way (and with identifiers following the same guidelines) as default fields. For example, a custom field described as
189
+
190
+```json
191
+"budget": {
192
+	"label": "Maximum budget allowed",
193
+	"type": "number",
194
+	"required": true
195
+}
196
+```
197
+in the settings file will result in
198
+```html
199
+<div id="form_budget">
200
+    <label for="form_budget_input">Maximum budget allowed</label>
201
+    <input required="required" placeholder="Maximum budget allowed" id="form_budget_input" type="number">
202
+</div>
203
+```
204
+
205
+Please note that the field's identifier ends with the field's tag name and not its type. For example, our `budget` field above will see its identifier become `form_budget_select` if it has the "select" type, `form_budget_textarea` if it has the "textarea" type, or `form_budget_input` for any other "type" value.
206
+
130 207
 Now it's all yours to make good use of all these identifiers and have a magnificient contact form :wink:
131 208
 
132 209
 I think that the code in itself is clear enough, if not please tell me so I can detail everything here!