, (*1)
, (*2)
A PHP rewrite of the fantastic Python library WTForms., (*3)
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)
Install
Add the following line to the require
portion of your composer.json
, (*5)
"deathnerd/php-wtforms":"0.5.2"
or if you're feeling froggy, pull in the cutting edge master release, (*6)
"deathnerd/php-wtforms":"dev-master"
or run the following command from your favorite terminal for the stable version, (*7)
composer require deathnerd/php-wtforms:0.5.2
or for the bleeding edge dev release, (*8)
composer require deathnerd/php-wtforms:dev-master
Note: The dev-master version is not guaranteed to be stable., (*9)
Quick Start
To create a simple login-form it's as simple as this:, (*10)
username = new StringField(["validators"=>[
new InputRequired("You must provide a username"),
new Length("Usernames must be between %(min)d and %(max)d characters long", ["min"=>3, "max"=>10]),
new NotContains("Usernames may not contain the following characters: ;-/@", ["invalid_members"=>[";","-","/","@"]])
]]);
$this->password = new PasswordField(["validators"=>[
new InputRequired("Can't log in without a password!"),
new Length("Passwords must be at least %(min)d characters in length", ["min"=>5])
]]);
$this->submit = new SubmitField(["label"=>"Submit"]);
}
}
```
### NotContains.php
```php
[]])
{
assert(!empty($options['invalid_members']), "Doesn't make sense to not have any invalid members");
$this->invalid_members = $options['invalid_members'];
$this->message = $message;
}
/**
* @param Form $form
* @param Field $field
* @param string $message
*
* @return mixed True if the field passed validation, a Validation Error if otherwise
* @throws ValidationError
*/
public function __invoke(Form $form, Field $field, $message = "")
{
if (strlen($field->data) != strlen(str_replace($this->invalid_members, "", $field->data))) {
if ($message == "") {
if ($this->message == "") {
$message = "Invalid Input.";
} else {
$message = $this->message;
}
}
throw new ValidationError($message);
}
return true;
}
}
```
### login.php
```php
$_POST]);
if ($_POST) {
if ($form->validate()) {
// do stuff to log in and authenticate the user
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>LogIn Form</title>
</head>
<body>
errors) { ?>
<ul class="errors">
<?php
foreach ($form->errors as $field_name => $errors) {
foreach ($errors as $field_error) { ?>
<li><?= $field_name ?> - <?= $field_error ?></li>
<?php
}
}
?>
</ul>
</body>
</html>
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, (*11)