PHP code example of estasi / form

1. Go to this page and download the library: Download estasi/form 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/ */

    

estasi / form example snippets



declare(strict_types=1);

use Estasi\Form\Field;
use Estasi\Form\Form;
use Estasi\Validator\Boolval;
use Estasi\Validator\Chain as ValidatorChain;
use Estasi\Validator\Email;
use Estasi\Validator\Identical;
use Estasi\Validator\Regex;


$loginValidator = (new ValidatorChain())->attach(new Boolval(Boolval::DISALLOW_STR_CONTAINS_ONLY_SPACE), ValidatorChain::WITH_BREAK_ON_FAILURE)
                                        ->attach(new Regex('[A-Za-z0-9_]{3,10}'), ValidatorChain::WITH_BREAK_ON_FAILURE);
$passwordValidator = (new ValidatorChain())->attach(new Boolval(Boolval::DISALLOW_STR_CONTAINS_ONLY_SPACE), ValidatorChain::WITH_BREAK_ON_FAILURE)
                                           ->attach(new Regex('((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[~!?_@#$%^&+-]).{6,15})'), ValidatorChain::WITH_BREAK_ON_FAILURE);
$passwordConfirmValidator = $passwordValidator->attach(new Identical('password[confirm]', Identical::STRICT_IDENTITY_VERIFICATION));
$emailValidator = (new ValidatorChain())->attach(new Boolval(Boolval::DISALLOW_STR_CONTAINS_ONLY_SPACE), ValidatorChain::WITH_BREAK_ON_FAILURE)
                                        ->attach(new Email(Email::ALLOW_UNICODE), ValidatorChain::WITH_BREAK_ON_FAILURE);

$login = new Field('login', Field::WITHOUT_FILTER, $loginValidator, Field::WITH_BREAK_ON_FAILURE, Field::WITHOUT_DEFAULT_VALUE, 'Login', 'Tooltip Login');
$password = new Field('password[first]', Field::WITHOUT_FILTER, $passwordValidator, Field::WITH_BREAK_ON_FAILURE, Field::WITHOUT_DEFAULT_VALUE, 'Password', 'Tooltip Password');
$passwordConfirm = new Field('password[confirm]', Field::WITHOUT_FILTER, $passwordConfirmValidator, Field::WITH_BREAK_ON_FAILURE, Field::WITHOUT_DEFAULT_VALUE, 'Password Confirm', 'Tooltip Password');
$email = new Field('email', Field::WITHOUT_FILTER, $emailValidator, Field::WITH_BREAK_ON_FAILURE, Field::WITHOUT_DEFAULT_VALUE, 'Email', 'Tooltip Email');

$formReg = new Form($login, $password, $passwordConfirm, $email);


declare(strict_types=1);

/** @var \Estasi\Form\Form $formReg */
$fields = \Estasi\Form\Utility\Fields::convertToArray($formReg->getFields());
/*
$fields = [
    'login' => [
        'name' => 'login',
        'label' => 'Login',
        'tooltip' => 'Tooltip Login',
        'select' => null,
        'errors' => [],
        'attributes' => [
            [
                'name' => 'login',
                '  "attributes": [{"name":"login","


declare(strict_types=1);

$requestedData = [
    'login' => 'Joe',
    'password[first]' => 'passwordJoe25!', 
    'password[confirm]' => 'passwordJoe25!', 
    'email' => '[email protected]'
];

/** @var \Estasi\Form\Form $formReg */
$formReg->setValues($requestedData);
if ($formReg->isValid()) {
    // We get all filtered and verified data of the same structure 
    // as the data passed for verification
    $validData = $formReg->getValues();
    // or Getting all valid Fields with the corresponding data
    $validFields = $formReg->getFieldsValid();
}


declare(strict_types=1);

use Estasi\Filter\Callback;
use Estasi\Filter\Chain;
use Estasi\Filter\Each;
use Estasi\Form\Field;
use Estasi\Form\Form;
use Estasi\Form\Utility\Fields;
use Estasi\Validator\Boolval;

$defaultValues= [
    'store_keywords[]' => ['keyword 1', 'keyword 2'],
];

$filterEachKeyword = new Each(new Chain(Chain::DEFAULT_PLUGIN_MANAGER, 'trim', 'lowercase'));
$filterKeywords = (new Chain())->attach($filterEachKeyword)
                               ->attach(new Callback(fn(array $val): array => array_filter($val, 'boolval')));
$store_keywords = new Field('store_keywords[]', $filterKeywords, new Boolval(), Field::WITH_BREAK_ON_FAILURE, $defaultValues['store_keywords[]'], 'Keywords', 'Tooltip Keywords');
$form = new Form($store_keywords);

$fields = Fields::convertToArray($form->getFields());
/*
$fields = [
    'store_keywords[]' => [
        'name' => 'store_keywords[]',
        'label' => 'Keywords',
        'tooltip' => 'Tooltip Keywords',
        'select' => null,
        'errors' => [],
        'attributes' => [
            [
                'name' => 'store_keywords[]',
                '


declare(strict_types=1);

use Estasi\Form\Field;
use Estasi\Form\Form;
use Estasi\Form\Option;
use Estasi\Form\Select;
use Estasi\Form\Utility\Fields;

// You can prepare the selection list by getting data from the database
/** @var \PDO $pdo */
$stm = $pdo->prepare(
    <<<SQL
    SELECT `name` `text`, JSON_OBJECT('value', `id`, 'title', `description`) `attributes`
    FROM `table`
    ORDER BY `id`;
    SQL
);
$stm->execute();
$optionsList = $stm->fetchAll(PDO::FETCH_CLASS, Option::class);

// or create a list manually
$optionsList = [
    new Option('Foo', ['value' => 1, 'title' => 'Foo it\'s ...']),
    new Option('Bar', ['value' => 2, 'title' => 'Bar great!']),
    new Option('Baz', ['value' => 3]),
];

$select = new Select(...$optionsList);
$type = new Field(
    'type', 
    Field::WITHOUT_FILTER, 
    Field::WITHOUT_VALIDATOR, 
    Field::WITH_BREAK_ON_FAILURE, 
    Field::WITHOUT_DEFAULT_VALUE, 
    'Type', 
    'Tooltip Type', 
    $select
);

$form = new Form($type);
$fields = Fields::convertToArray($form->getFields());
/*
$fields = [
    'type' => [
        'name' => 'type',
        'label' => 'Type',
        'tooltip' => 'Tooltip Type',
        'select' => [
            [
                'text' => 'Foo',
                'attributes' => ['title' => 'Foo it\'s ...', 'value' => 1]
            ],
            [
                'text' => 'Bar',
                'attributes' => ['title' => 'Bar great!', 'value' => 2]
            ],
            [
                'text' => 'Baz',
                'attributes' => ['value' => 3]
            ],
        ],
        'errors' => [],
        'attributes' => [
            [
                'name' => 'type',
                'value'=> null,
            ],
        ],
    ],
];
*/