1
1
[ ![ Build Status] ( https://travis-ci.org/Deathnerd/php-wtforms.svg?branch=master )] ( https://travis-ci.org/Deathnerd/php-wtforms )
2
2
# php-wtforms
3
- A PHP rewrite of the fantastic Python library WTForms
3
+ A PHP rewrite of the fantastic Python library WTForms.
4
+
5
+ I do not take credit for the idea behind WTForms or anything related to its original implementation. I just bastardized it and ported it to PHP.
4
6
5
7
# Install
6
8
Add the following line to the ` require ` portion of your ` composer.json `
@@ -11,4 +13,87 @@ or run the following command from your favorite terminal
11
13
` composer require deathnerd/php-wtforms:dev-master 0.5.* `
12
14
13
15
# Quick Start
14
- Still todo, but look at the tests for a gist of how you can use this library
16
+ To create a simple login-form it's as simple as this:
17
+ ###LogInForm.php
18
+ ``` php
19
+ <?php
20
+ namespace MyNamespace\Forms;
21
+
22
+ use WTForms\Form;
23
+ use WTForms\Fields\Simple\PasswordField;
24
+ use WTForms\Fields\Simple\SubmitField;
25
+ use WTForms\Fields\Core\StringField;
26
+ use WTForms\Validators\InputRequired;
27
+ use WTForms\Validators\Length;
28
+ use WTForms\Validators\Regexp;
29
+
30
+ class LogInForm extends Form {
31
+ public function __construct(array $options)
32
+ {
33
+ parent::__construct($options);
34
+ $this->username = new StringField(["validators"=>[
35
+ new InputRequired("You must provide a username"),
36
+ new Length("Usernames must be between %(min) and %(max) characters long", ["min"=>3, "max"=>10]),
37
+ new Regexp("Usernames may not contain the following characters: ;-/@", ["regex"=>'/^((?!;\\-\\/@).)*$/'])
38
+ ]]);
39
+ $this->password = new PasswordField(["validators"=>[
40
+ new InputRequired("Can't log in without a password!"),
41
+ new Length("Passwords must be at least %(min) characters in length", ["min"=>5])
42
+ ]]);
43
+ $this->submit = new SubmitField(["label"=>"Submit"]);
44
+ }
45
+ }
46
+ ```
47
+ ###login.php
48
+ ``` php
49
+ <?php
50
+ require_once 'vendor/autoload.php';
51
+
52
+ use MyNamespace\Forms\LogInForm;
53
+
54
+ $form = LogInForm::create(["formdata" => $_POST]);
55
+
56
+ if ($_POST) {
57
+ if ($form->validate()) {
58
+ // do stuff to log in and authenticate the user
59
+ }
60
+ }
61
+ ?>
62
+ <!doctype html>
63
+ <html lang =" en" >
64
+ <head >
65
+ <meta charset =" UTF-8" >
66
+ <meta name =" viewport"
67
+ content =" width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >
68
+ <meta http-equiv =" X-UA-Compatible" content =" ie=edge" >
69
+ <title >LogIn Form</title >
70
+ </head >
71
+ <body >
72
+ <?php
73
+ if ($form->errors) {
74
+ ?>
75
+ <ul class =" errors" >
76
+ <?php
77
+ foreach ($form->errors as $field_name => $errors) {
78
+ foreach ($errors as $field_error) { ?>
79
+ <li ><?= $field_name ?> - <?= $field_error ?></li >
80
+ <?
81
+ }
82
+ }
83
+ ?>
84
+ </ul >
85
+ <?php
86
+ }
87
+ ?>
88
+ <form action =" <?= $_SERVER['PHP_SELF'] ?>" method =" POST" >
89
+ <?= $form->username->label ?>
90
+ <?= $form->username ?>
91
+ <?= $form->password->label ?>
92
+ <?= $form->password ?>
93
+ <?= $form->submit ?>
94
+ </form >
95
+ </body >
96
+ </html >
97
+ ```
98
+
99
+ And that's that. More in-depth examples and actual documentation are coming in the future. For now, look in the ` tests ` directory for ideas on how to do things
0 commit comments