PHP code example of deathnerd / php-wtforms

1. Go to this page and download the library: Download deathnerd/php-wtforms library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

deathnerd / php-wtforms example snippets



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"]);
    }
}


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;
    }
}


MyNamespace\Forms\LogInForm;

$form = LogInForm::create(["formdata" => $_POST]);

if ($_POST) {
    if ($form->validate()) {
        // do stuff to log in and authenticate the user
    }
}
json
"deathnerd/php-wtforms":"0.5.2"
json
"deathnerd/php-wtforms":"dev-master"