You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+36-35Lines changed: 36 additions & 35 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,14 +6,14 @@ Small library to validate simple html forms data and send requests to email.
6
6
Furthermore you can write your own "handler" to process valid data, for example if you need to save
7
7
it through API to a 3d-party service like Mailchimp, SalesForce, CRM system, etc.).
8
8
9
-
##Why FormHandler
9
+
# Why FormHandler
10
10
11
11
It's very easy to find some ready-to-use solution to process a contact form. Usually this is pure PHP
12
12
script, which collect data and send email with php `mail()` function. It's not bad, but you can find
13
13
numerous problems with such scripts:
14
14
15
15
*`mail()` function can be blocked on production server, because it's not secure. Also it's often goes to SPAM folder, when you use `mail()` function.
16
-
* You need to validate, that the data is valid. Manual validation of the `$_POST` array is time consuming and require knowledge of PHP, RegExp's knowledge etc.
16
+
* You need to validate, that the data is valid. Manual validation of the `$_POST` array is time consuming and require knowledge of PHP, RegExp's, etc.
17
17
18
18
We decide to create small library, which fix all these issues, so to process a form you need:
19
19
@@ -23,23 +23,19 @@ We decide to create small library, which fix all these issues, so to process a f
23
23
24
24
And that's it!
25
25
26
-
##Requirements
26
+
# Requirements
27
27
28
28
* PHP 7.0+
29
-
*[Composer](http://getcomposer.org/)
29
+
*[Composer](http://getcomposer.org/download)
30
+
* Working SMTP server to send emails, or Mandrill account with configured mail domain.
30
31
31
-
##Usage
32
+
# Usage
32
33
33
34
Imagine you have simple html website with a contact form and you want to process it.
34
35
We have `name`, `email`, and `message` form fields.
35
36
We will guide you through the whole process of creating PHP script to process a form request.
36
37
37
-
### Examples
38
-
39
-
You can check working examples inside `examples` folder of the package, start your investigate from `index.php` file (contains forms HTML).
40
-
There you can find which files loaded next, when you submit forms.
41
-
42
-
### Init your environment
38
+
## 1. Init your environment
43
39
44
40
We suggest to create separate folder to place code into it. Let's call it `form`.
45
41
File structure will looks like this:
@@ -51,15 +47,15 @@ Inside `/form/` folder we need to create `composer.json` file to set our library
51
47
52
48
{
53
49
"require": {
54
-
"justcoded/form-handler": "^1.0.*"
50
+
"justcoded/form-handler": "*"
55
51
}
56
52
}
57
53
58
54
Now we need to download all required files with a composer, by running a bash command:
59
55
60
56
composer install
61
57
62
-
### 3. Entry file
58
+
##2. Entry file
63
59
64
60
You must create entry file, which will handle the form request.
65
61
You can copy one of our examples `examples/basic.php` or `examples/advanced.php` inside package folder.
@@ -80,7 +76,7 @@ use JustCoded\FormHandler\DataObjects\MailMessage;
80
76
use JustCoded\FormHandler\FileManager\FileManager;
81
77
```
82
78
83
-
### 4. Form processing
79
+
##3. Form processing
84
80
85
81
Form processing idea is super easy. We have main `FormHandler` object, which will validate data and
86
82
run some handler (right now we have only one Handler - email sender). And as the result we can get info
@@ -101,15 +97,15 @@ $result = $form->response();
101
97
// TODO: do somethign with the results. For example write to a session and redirect back.
102
98
```
103
99
104
-
### 5. Set Configurations
100
+
##4. Set Configurations
105
101
106
102
As you can see above we need to set 3 configuration arrays:
107
103
108
104
*`$validationRules` - defines validation rules and messages
109
105
*`$mailerConfig` - defines mailer component (PHPMailer or Mailchimp) and it's params
110
106
*`$messageConfig` - defines From/To/Body fields
111
107
112
-
#### 5.1. Validation Rules
108
+
###4.1. Validation Rules
113
109
114
110
For validation we use popular [Valitron](https://github.com/vlucas/valitron) PHP library. We use
115
111
`mapFieldsRules()` method to set fields rules and `labels()` method to set field labels to show
@@ -135,7 +131,7 @@ $validationRules = [
135
131
];
136
132
```
137
133
138
-
#### 5.2. Mailer Config
134
+
###4.2. Mailer Config
139
135
140
136
There are two options for Mailer: [PHPMailer](https://github.com/PHPMailer/PHPMailer) and implementation
141
137
of [Mandrill API](https://mandrillapp.com/api/docs/).
@@ -163,9 +159,9 @@ $mailerConfig = [
163
159
];
164
160
```
165
161
166
-
#### 5.3. Message configuration
162
+
###4.3. Message configuration
167
163
168
-
Final configuration you have to set is options for your email: From, To addresses; Subject and Body.
164
+
The latest configuration you have to set is options for your email: From, To addresses; Subject and Body.
169
165
Optional you can set CC and BCC headers as well.
170
166
171
167
Example:
@@ -182,16 +178,16 @@ $messageConfig = [
182
178
];
183
179
```
184
180
185
-
For each address you can set numerous emails in such format:
181
+
For each address field you can set numerous emails in such format:
186
182
187
183
[ email1 => name1, email2 => name2, ... ]
188
184
OR
189
185
[email1, email2, email3 ...]
190
186
191
-
`bodyTemplate` and `altBodyTemplate` are path to usual PHP template files, which will be used to generate
192
-
message body part.
187
+
`bodyTemplate` and `altBodyTemplate` are paths to usual PHP template files, which will be used to generate
188
+
email message (HTML and plain versions accordingly).
193
189
194
-
### 6. All together
190
+
##5. All together
195
191
196
192
If we combine all parts we can get file similar to this one:
197
193
@@ -256,7 +252,7 @@ exit;
256
252
257
253
In this example we write errors to cookies to be able to get them on the HTML page via JavaScript or PHP code.
258
254
259
-
### 7. Body templates
255
+
##6. Body templates
260
256
261
257
Templates are usual PHP files, which can print any PHP code you leave inside. However to make editing
262
258
easier we added tokens support. So any keys, which are passed as data to FormHandler can be used as a
@@ -453,17 +449,17 @@ Some forms may have multiple fields, like checkboxes, multiple selects or dynami
453
449
Example:
454
450
455
451
```html
456
-
<!-- multiple select -->
457
-
<selectname="choice"multiple>
458
-
<optionvalue="1">1</option>
459
-
<optionvalue="2">2</option>
460
-
<optionvalue="3">3</option>
461
-
</select>
462
-
463
-
<!-- text inputs -->
464
-
<inputtype="text"name="links[]">
465
-
<inputtype="text"name="links[]">
466
-
<inputtype="text"name="links[]">
452
+
<!-- multiple select -->
453
+
<selectname="choice"multiple>
454
+
<optionvalue="1">1</option>
455
+
<optionvalue="2">2</option>
456
+
<optionvalue="3">3</option>
457
+
</select>
458
+
459
+
<!-- text inputs -->
460
+
<inputtype="text"name="links[]">
461
+
<inputtype="text"name="links[]">
462
+
<inputtype="text"name="links[]">
467
463
```
468
464
469
465
You can validate each input using wildcard field name inside validation rules:
@@ -511,3 +507,8 @@ field in curly braces. For example, in the file 'template-html.php':
511
507
</body>
512
508
</html>
513
509
```
510
+
511
+
# Examples
512
+
513
+
You can check working examples inside `examples` folder of the package, start your investigate from `index.php` file (contains forms HTML).
514
+
There you can find which files loaded next, when you submit forms.
0 commit comments