PHP code example of moon250 / form-generator

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

    

moon250 / form-generator example snippets


// quire_once 'vendor/autoload.php';
$form = new \FormGenerator\FormGenerator();

$form = new \FormGenerator\FormGenerator();
// This method add a field named "username" with default type (text)
$form->add('username');
// Add method is fluent
$form->add('username')->add('name');

$form = new \FormGenerator\FormGenerator();

// Generate method will return only form fields (input, select, ...)
$html = $form->add('username')->generate();

// <input type="text" id="field-username" name="username" value="" 

$form = new \FormGenerator\FormGenerator();

// Will generate an input with "email" type.
// <input type="email" id="field-user-email" name="user-email" value="" 

$form = new \FormGenerator\FormGenerator();

// The input is generated with a date type
// <input type="date" id="field-created_at" name="created_at" value="" ;

$form = new \FormGenerator\FormGenerator();

// <input type="email" id="field-email" name="email" value=""> 
$form->add('email')->generate();

$form = new \FormGenerator\FormGenerator();

// <input type="text" id="field-username" name="username" value="" 

$config = new \FormGenerator\FormConfig();
$config->set('type_detection', false);
$form = new \FormGenerator\FormGenerator($config);

// <input type="text" id="field-email" name="email" value="" 

$config = new \FormGenerator\FormConfig();
$config->get('type_detection'); // true

$config = new \FormGenerator\FormConfig();
$config->set('TYPE_DETECTION', false);

// Echo "false"
echo $config->get('type_detection');

$config = new \FormGenerator\FormConfig([
    'type_detection' => false,
    'empty_generated_fields' => false
]);

// Echo "false"
echo $config->get('type_detection');
// Echo "false"
echo $config->get('empty_generated_fields');

$config = new \FormGenerator\FormConfig([
    'empty_generated_field' => true // default is true
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');
// <input type="text" id="field-username" name="username" value="" 

$config = new \FormGenerator\FormConfig([
    'full_html_structure' => true,
    'form_action' => '/home'
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');
$form->generate(); // <form action="/home" method="POST">...

$config = new \FormGenerator\FormConfig([
    'form_class' => 'super-class',
    'full_html_structure' => true
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');
$form->generate(); // <form action="" method="POST" class="super-class">...

$config = new \FormGenerator\FormConfig([
    'full_html_structure' => true,
    'form_method' => 'GET'
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');
$form->generate(); // <form action="" method="GET">...

$config = new \FormGenerator\FormConfig([
    'full_html_structure' => true,
    'form_submit' => true
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');

// <form method="POST" action="">
//     <input type="text" id="field-username" name="username" value="" 

$config = new \FormGenerator\FormConfig([
    'full_html_structure' => true,
    'form_submit' => true,
    'form_submit_value' => 'Send !'
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');

// <form method="POST" action="">
//     <input type="text" id="field-username" name="username" value="" 

$config = new \FormGenerator\FormConfig([
    'full_html_structure' => true
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('username');
$form->generate(); // <form action="" method="POST">...</form>

$config = new \FormGenerator\FormConfig([
    'type_detection' => true // default is true
]);

$form = new \FormGenerator\FormGenerator($config);
$form->add('password');
// <input type="password" id="field-password" name="password" value="" 


    
$config = new \FormGenerator\FormConfig([
    'full_html_structure' => true,
    'form_submit_value' => 'Login',
    'form_action' => '/home'
]);

$generator = new FormGenerator\FormGenerator($config);

$form = $generator
    ->add('username', null, [
    'label' => 'Your username',
    'placeholder' => 'Username',
    'class' => 'form-control'
])
    ->add('password', null, [
    'label' => 'Your password',
    'placeholder' => 'Password',
    'class' => 'form-control'
])->generate();

// <form method="POST" action="/home">
//     <label for="field-username">Your username</label>
//     <input type="text" id="field-username" name="username" value="" placeholder="Username"