PHP code example of iyoworks / form-builder

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

    

iyoworks / form-builder example snippets


$form = FormBuilder::form();
$form->action('UserController@create')
//$form->add[FieldType]([field_slug] [, Field Label]);
$form->addText('first_name', 'First Name');
$form->addSelect('gender')->options(['male'=>'Male', 'female'=>'Female', 'none'=>'Not Telling']);

$form->render();

// Set field with id 'gender' to have 3 options instead of 2.
$form->getField('gender')->options(['m'=>'Male', 'f'=>'Female', 'n'=>'Not Telling']);

// Remove the gender field
$form->remove('gender');

// Add last name after first name
$form->addAfter('first_name', 'last_name', 'text');
$form->addTextAfterFirstName('last_name');
$form->addBefore('last_name', 'first_name', 'text');
$form->addTextAfterLastName('first_name');

use Iyoworks\FormBuilder\Form;
use Iyoworks\FormBuilder\Field;
// Closure support for FormBuilder
$form = FormBuilder::form(function(Form $form) {
    $form->url('users/create');
	$form->addText('first_name');
    $form->addSelect('gender'->options(['M'=>'Male', 'F'=>'Female']);
})->html();

echo $form->open(), $form->render(), $form->close();
# the same as
echo $form->html();

$form->addRow(function($form){
    $form->addText('first_name', 'First Name')
    	->label('First Name')
    	->description('Enter your first name')
    	->columns(12);
    $form->addText('last_name', 'Last Name')
        ->label('First Name')
        ->description('Enter your last name')
        ->columns(12);
});
$form->addEmail('email', 'Email Address')
$form->addSubmit('Submit')->addClass('btn btn-block btn-primary');

beforeForm(Form $form)
afterForm(Form $form)
beforeField(Form $form, Field $field)
afterField(Form $form, Field $field)

// Per-form Callbacks
$form->beforeField(function(Form $form, Field $field) {
	// Use field settings to display your form nicely
	return '<label>' . $field->label . '</label>';
});

// Global form callbacks
FormBuilder::bind('beforeField', function(Form $form, Field $field) {
		return '<div class="form-group"><label>'.$field->label.'</label>';
	})
	->bind('afterField', function(Form $form, Field $field) {
		$output = '';
        if ( $field->description )
            $output .= '<p class="help-block">' . $field->description . '</p>';
        return $output . '</div>';
	});
$form = FormBuilder::form(function(Form $form) {
    $form->route('user.create');
	$form->addText('first_name', 'First Name');
	$form->addText('last_name')->label('Last Name');
});

echo $form->model($model)->html();