Skip to content
This repository was archived by the owner on Feb 18, 2025. It is now read-only.

Commit 3b2c8c0

Browse files
committed
Added a simple example to the README
1 parent eb6e422 commit 3b2c8c0

File tree

1 file changed

+87
-2
lines changed

1 file changed

+87
-2
lines changed

README.md

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[![Build Status](https://travis-ci.org/Deathnerd/php-wtforms.svg?branch=master)](https://travis-ci.org/Deathnerd/php-wtforms)
22
# 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.
46

57
# Install
68
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
1113
`composer require deathnerd/php-wtforms:dev-master 0.5.*`
1214

1315
# 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

Comments
 (0)