PHP code example of progphil1337 / php-forms

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

    

progphil1337 / php-forms example snippets


use ProgPhil1337\Forms\Element\Input;
use ProgPhil1337\Forms\Element\Radio;
use ProgPhil1337\Forms\Element\Select;
use ProgPhil1337\Forms\Enum\InputType;
use ProgPhil1337\Forms\Form;
use ProgPhil1337\Forms\Validation\Validator\MaxLength;
use ProgPhil1337\Forms\Validation\Validator\MinLength;
use ProgPhil1337\Forms\Validation\Validator\IsRequired;
use ProgPhil1337\Forms\Enum\RequestMethod;

class ExampleForm extends Form
{

    public function __construct()
    {
        parent::__construct('example-form', RequestMethod::POST);
    }

    protected function build(): void
    {
        $username = new Input('username', InputType::TEXT, 'Username');
        $username->addValidator(new MaxLength(1));
        $username->addValidator(new MinLength(1));
        $username->addValidator(new IsRequired(true));
        $this->add($username);

        $mail = new Input('mail', InputType::EMAIL, 'E-Mail');
        $this->add($mail);

        $radio = new Radio('language', [
            'php' => 'PHP',
            'csharp' => 'C-Sharp'
        ], 'Favorite Language');
        $radio->setValue('php');
        $this->add($radio);

        $select = new Select('car', [
            'volvo' => 'Volvo',
            'vw' => 'VW',
            'bmw' => 'BMW',
            'audi' => 'Audi'
        ], 'Favorite car');

        $select->setValue('audi'); // obviously
        $this->add($select);

        $this->submitButton('Speichern');
    }
}
 
$form = new ExampleForm();

$form->setDefaultValues([
    'mail' => '[email protected]'
]);

$errorMessages = [];

if ($request->method->value === $form->method->value) {
    $result = $form->validate($request->body);
    $valid = $result->isValid();

    if (!$valid) {
        foreach ($result->getErrorMessages() as $inputName => $validators) {
            $errorMessages[$inputName] = [];
            foreach ($validators as $info) {
                $errorMessages[$inputName][] = $info['message'];
            }
        }
    }
}

someRenderFunction('someTemplate.twig', [
    'form' => $form,
    'errorMessages' => $errorMessages
])
bash
$ composer 
twig 
{{ form.openTag()|raw }}
{{ errorMessages["username"]|join('<br />') }}
{{ form.get("username").label|raw }}<br />
{{ form.get("username").element|raw }}<br />
<br />
{{ errorMessages["mail"]|join('<br />') }}
{{ form.get("mail").label|raw }}<br />
{{ form.get("mail")|raw }}<br />
...

{{ form.get("submit").element|raw }}
{{ form.closeTag()|raw }}

{# you can also just do form|raw to auto generate the html #}

{{ form|raw }}