GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Deathnerd/php-wtforms: A PHP rewrite of the fantastic Python library WTForms

BranchesTags
Open more actions menu

Latest commit

 

History

308 Commits
308 Commits

Folders and files

NameName
Last commit message
Last commit date
.claude
.claude
 
 
.devcontainer
.devcontainer
 
 
.github/workflows
.github/workflows
 
 
.kittify
.kittify
 
 
bin
bin
 
 
commands
commands
 
 
docs/plans
docs/plans
 
 
kitty-specs/001-typed-options-migration
kitty-specs/001-typed-options-migration
 
 
phpstan
phpstan
 
 
scripts
scripts
 
 
src
src
 
 
tests
tests
 
 
.claudeignore
.claudeignore
 
 
.editorconfig
.editorconfig
 
 
.gitattributes
.gitattributes
 
 
.gitignore
.gitignore
 
 
.php-cs-fixer.dist.php
.php-cs-fixer.dist.php
 
 
CHANGELOG.md
CHANGELOG.md
 
 
CLAUDE.md
CLAUDE.md
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
ROADMAP.md
ROADMAP.md
 
 
TODO.md
TODO.md
 
 
composer.bat
composer.bat
 
 
composer.json
composer.json
 
 
composer.lock
composer.lock
 
 
php-cs-fixer.bat
php-cs-fixer.bat
 
 
php.bat
php.bat
 
 
phpstan.bat
phpstan.bat
 
 
phpstan.neon
phpstan.neon
 
 
phpunit.xml
phpunit.xml
 
 
rector.bat
rector.bat
 
 
rector.php
rector.php
 
 

Repository files navigation

php-wtforms

A PHP rewrite of the fantastic Python library WTForms.

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.

Install

Add the following line to the require portion of your composer.json

"deathnerd/php-wtforms":"0.5.2"

or if you're feeling froggy, pull in the cutting edge master release

"deathnerd/php-wtforms":"dev-master"

or run the following command from your favorite terminal for the stable version

composer require deathnerd/php-wtforms:0.5.2

or for the bleeding edge dev release

composer require deathnerd/php-wtforms:dev-master

Note: The dev-master version is not guaranteed to be stable.

Quick Start

To create a simple login-form it's as simple as this:

LogInForm.php

<?php
namespace MyNamespace\Forms;

use WTForms\Form;
use WTForms\Fields\Simple\PasswordField;
use WTForms\Fields\Simple\SubmitField;
use WTForms\Fields\Core\StringField;
use WTForms\Validators\InputRequired;
use WTForms\Validators\Length;
use WTForms\Validators\Regexp;
use MyNamespace\Validators\NotContains;

class LogInForm extends Form {
    public function __construct(array $options = [])
    {
        parent::__construct($options);
        $this->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
namespace MyNamespace\Validators;

use WTForms\Validators\Validator;
use WTForms\Form;
use WTForms\Fields\Core\Field;
use WTForms\Exceptions\ValidationError;

class NotContains extends Validator
{
    /**
     * @var string|array
     */
    public $invalid_members;

    /**
     * @param string $message Error message to raise in case of a validation error
     * @param array  $options
     */
    public function __construct($message = "", array $options = ['invalid_members' => []])
    {
        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
require_once 'vendor/autoload.php';

use MyNamespace\Forms\LogInForm;

$form = LogInForm::create(["formdata" => $_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>
<?php if ($form->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>
<?php } ?>
<form action="<?= $_SERVER['PHP_SELF'] ?>" method="POST">
    <?= $form->username->label ?>
    <?= $form->username ?>
    <?= $form->password->label ?>
    <?= $form->password ?>
    <?= $form->submit ?>
</form>
</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

About

A PHP rewrite of the fantastic Python library WTForms

Topics

phpwtforms

Resources

Readme
GPL-2.0 license
Activity

Stars

4 stars

Watchers

2 watching

Forks

3 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.