1. Go to this page and download the library: Download rafaelwendel/ci4formbuilder 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/ */
rafaelwendel / ci4formbuilder example snippets
namespace App\Controllers;
use CI4FormBuilder\Form;
use CI4FormBuilder\Input;
use CI4FormBuilder\Label;
use CI4FormBuilder\Submit;
class Home extends BaseController
{
public function index()
{
$form = new Form(); //the same constructor params of CI4 "form_open" function can be used
//create an input to "firstname"
$firstNameField = new Input('firstname'); //the same constructor params of CI4 "form_input" function
//set a label to component
$firstNameField->setLabel(new Label('First Name: ', 'firstName'));
//create an input to "lastname"
$lastNameField = new Input('lastname');
$lastNameField->setLabel(new Label('Last Name: ', 'lastName'));
$submitButton = new Submit('btnSave', 'Save');
//add components to $form
$form->addComponent($firstNameField)
->addComponent($lastNameField)
->addComponent($submitButton);
//display form
echo $form->display();
}
}
namespace App\Controllers;
use CI4FormBuilder\Checkbox;
use CI4FormBuilder\Dropdown;
use CI4FormBuilder\Form;
use CI4FormBuilder\Input;
use CI4FormBuilder\Label;
use CI4FormBuilder\Password;
use CI4FormBuilder\Submit;
class Home extends BaseController
{
public function index()
{
$form = new Form();
//create an input to "firstname"
$nameField = new Input('name');
$nameField->setLabel(new Label('Name: ', 'name'));
//create an input type email to "email"
$emailField = new Input('email', '', '', 'email'); //last param is type of input
$emailField->setLabel(new Label('Email: ', 'email'));
$genderField = new Dropdown('gender', ['M' => 'Male', 'F' => 'Female']);
$genderField->setLabel(new Label('Gender: ', 'gender'));
$passField = new Password('password');
$passField->setLabel(new Label('Password: ', 'password'));
$checkTerms = new Checkbox('terms', 'Accept');
$checkTerms->setLabel(new Label('Agree the terms', 'terms'));
$submitButton = new Submit('btnSave', 'Save');
//add components to $form (pass an array with all components)
$form->addComponent([$nameField, $emailField, $genderField, $passField, $checkTerms, $submitButton]);
//display form
echo $form->display();
}
}
namespace App\Controllers;
use CI4FormBuilder\Form;
use CI4FormBuilder\Input;
use CI4FormBuilder\Label;
use CI4FormBuilder\Password;
use CI4FormBuilder\Submit;
use CI4FormBuilder\Template;
class Home extends BaseController
{
public function index()
{
$form = new Form();
//lets set the Template options
$template = new Template([
'beforeForm' => '<div class="form-login">',
'afterForm' => '</div>',
'beforeComponent' => '<div class="form-field">',
'afterComponent' => '</div>',
'inputExtra' => 'class="form-input"', //pass extra in a string
'submitExtra' => ['id' => 'btnLogin', 'class' => 'btn'], //pass extra in an array
]);
//define the Form template
$form->setTemplate($template);
//create an input to "username"
$usernameField = new Input('username');
$usernameField->setLabel(new Label('Username: ', 'username'));
//create an input to "password"
$passField = new Password('password');
$passField->setLabel(new Label('Password: ', 'password'));
$submitButton = new Submit('btnLogin', 'Login');
//add components to $form
$form->addComponent([$usernameField, $passField, $submitButton]);
//display form
echo $form->display();
}
}
$nameField = new Input('name', 'Michael Jordan');
$nameField->setLabel(new Label('Name: ', 'name'));
$emailField = new Input('email', '[email protected]', '', 'email'); //last param is type of input
$emailField->setLabel(new Label('Email: ', 'email'));
/*
Output:
<label for="name">Name: </label>
<input type="text" name="name" value="Michael Jordan">
<label for="email">Email: </label>
<input type="email" name="email" value="[email protected]">
*/
$form = new Form();
//Customers form
$firstNameField = new Input('firstname'); /
$firstNameField->setLabel(new Label('First Name: ', 'firstName'));
$lastNameField = new Input('lastname');
$lastNameField->setLabel(new Label('Last Name: ', 'lastName'));
$submitButton = new Submit('btnSave', 'Save');
//add components to $form
$form->addComponent($firstNameField)
->addComponent($lastNameField)
->addComponent($submitButton);
//set data from db
$customerToEdit = model('CustomerModel')->find(1); //an object or array with "firstname" and "lastname" params/keys.
$form->setFormData($customerToEdit);
//to set data from request
$form->setFormData($this->request->getPost());
//display form
echo $form->display();
//define validation rules
$errors = [];
$validation = \Config\Services::validation();
if($this->request->is('post')) {
$validation->setRules([
'name' => ' new Form();
//set errors messages
$form->setErrorsValidation($errors);
//Template options to display errors
$template = new Template([
'beforeErrorMessage' => '<span style="color: red">',
'afterErrorMessage' => '</span>',
]);
//define the Form template
$form->setTemplate($template);
//create an input to "username"
$nameField = new Input('name');
$nameField->setLabel(new Label('Name: ', 'name'));
//create an input to "email"
$emailField = new Input('email', '', '', 'email');
$emailField->setLabel(new Label('Email: ', 'email'));
$submitButton = new Submit('btnSave', 'Save');
//add components to $form
$form->addComponent([$nameField, $emailField, $submitButton]);
//display form
echo $form->display();
namespace App\FormTypes;
use CI4FormBuilder\FormTypeAbstract;
use CI4FormBuilder\Hidden;
use CI4FormBuilder\Input;
use CI4FormBuilder\Label;
use CI4FormBuilder\Submit;
class ProductType extends FormTypeAbstract
{
//implement the abastract method defined on super
protected function setComponents()
{
$hiddenId = new Hidden('id');
$inputName = new Input('name');
$inputName->setLabel(new Label('Name: ', 'name'));
$inputPrice = new Input('price');
$inputPrice->setLabel(new Label('Price: ', 'price'));
$submitButton = new Submit('btnSave', $this->getSubmitLabel());
$this->addComponent([$hiddenId, $inputName, $inputPrice, $submitButton]);
}
}
namespace App\Controllers;
use App\FormTypes\ProductType;
class Products extends BaseController
{
/* ...
construct and others methods
...
*/
public function add()
{
$productType = new ProductType();
//set the submit label
$productType->setSubmitLabel('Save');
$data['form'] = $productType->display();
return view('products_form', $data);
}
}
public function edit($id)
{
$product = model('ProductModel')->find($id);
$productType = new ProductType();
//set the submit label
$productType->setSubmitLabel('Edit/Update');
//set data
$productType->setFormData($product);
$data['form'] = $productType;
return view('products_form', $data);
}