PHP code example of alessandrodesign / formbuilder

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

    

alessandrodesign / formbuilder example snippets




use AlessandroDesign\FormBuilder\Enums\EnctypeEnum;
use AlessandroDesign\FormBuilder\Enums\MethodEnum;
use AlessandroDesign\FormBuilder\Form;




use AlessandroDesign\FormBuilder\Enums\EnctypeEnum;
use AlessandroDesign\FormBuilder\Enums\MethodEnum;
use AlessandroDesign\FormBuilder\Form;




$form->render();



$form;



use AlessandroDesign\FormBuilder\Form;

Teste',
        MethodEnum::POST,
        'https://localhost:8080/post',
        EnctypeEnum::MultipartFormData
    )->setClass('row g-3');

    // Adiciona campos de texto, radio, checkbox e arquivos
    $form->inputText('Nome', 'nome', placeholder: 'Digite seu nome')
        ->addClassElement('form-control')
        ->addParent('col-md-6', 'input-group');

    $form->inputRadio('Gênero', 'genero', options: ['Masculino', 'Feminino'])
        ->addClassElement('form-check-input')
        ->addParent('col-md-6', 'input-group');

    $form->inputCheckbox('Preferências', 'preferencias', options: ['Opção 1', 'Opção 2'])
        ->addClassElement('form-check-input')
        ->addParent('col-md-6', 'input-group');

    $form->inputFile('Anexar Arquivo', 'anexo')
        ->addClassElement('form-control')
        ->addParent('col-md-12', 'input-group');

    echo $form->render();
} catch (DOMException $e) {
    echo $e->getMessage();
}



use AlessandroDesign\FormBuilder\Form;

$form = Form::create(
    'formSelect',
    MethodEnum::POST,
    'https://localhost:8080/post'
);

$form->select('Escolha uma opção', 'opcao')
    ->addOptions([
        'Grupo 1' => ['valor1' => 'Opção 1', 'valor2' => 'Opção 2'],
        'Grupo 2' => ['valor3' => 'Opção 3', 'valor4' => 'Opção 4']
    ])
    ->addClassElement('form-select')
    ->addParent('col-md-12');

echo $form->render();



$form->button('Enviar', 'submit', 'btn btn-primary', 'Enviar');
echo $form->render();

$form->useToken();

if (Form::validateToken()) {
    echo 'Token válido';
} else {
    throw new Exception('Token inválido');
}