PHP code example of eredi93 / forms

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

    

eredi93 / forms example snippets


use Forms\From;
use Forms\Validators\Required;

class LoginForm extends Form
{
    protected function setUp()
    {
        $this->fields = [
            (new TagOpen(['class' => "wrapper"])),
            (new TextInput("identifier"))
                ->setLabel("User / Email")
                ->setAutocomplete("off")
                ->setDecoratorClass("input-field col s12")
                ->setValidators([
                    new Required(),
                ]),
            (new PasswordInput("password"))
                ->setLabel("Password")
                ->setDecoratorClass("input-field col s12")
                ->setValidators([
                    new Required(),
                ]),
            (new SubmitButton())
                ->setDecoratorClass("input-field col s12")
                ->setClass("btn yellow right")
                ->setText("Login"),
            (new TagClose(),
        ];
    }
}

public function login(Request $request, Response $response, $args)
{
    // Instantiate Form
    $form = new LoginForm();
    /**
     * You can pass to the form the method action and the CSRF name
     * by default the form uses
     * - $action=""
     * - $method="POST"
     * - $csrf=['csrf_name', 'csrf_value']
     */
    // Render Form
    echo $form->render($request);

    return $response;
}

public function loginPost(Request $request, Response $response, $args)
{
    // Instantiate Form
    $form = new LoginForm();

    // Check validation
    if ($form->validate($request)) {
        /*
        *   Form validation passed log in the user
        */
    }

    // Render Form with validation
    echo $form->render($request);

    return $response;
}