PHP Classes

FormOne: Generate HTML form composed programatically

Recommend this page to a friend!
  Info   View files Example   View files View files (9)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 176 This week: 1All time: 8,760 This week: 571Up
Version License PHP version Categories
formone 1.0.1GNU Lesser Genera...5HTML, PHP 5
Description 

Author

This class can generate HTML form composed programatically.

It can compose a form adding field elements using a fluent interface.

The class can add form inputs and the respective labels and attributes and the form inputs can be rendered individually.

Picture of Jorge Castro
  Performance   Level  
Name: Jorge Castro <contact>
Classes: 30 packages by
Country: Chile Chile
Age: 48
All time rank: 12763 in Chile Chile
Week rank: 103 Up1 in Chile Chile Up
Innovation award
Innovation award
Nominee: 14x

Winner: 2x

Example

<!doctype html>
<html>
<?php
use eftec\MessageList;
use
eftec\ValidationOne;
include
"common.php";

$f=new \eftec\FormOne();

echo
$f->start();
echo
$f->id('field1')
    ->
label('field 1:')
    ->
type('label')
    ->
render();
echo
$f->id('field1')
    ->
type('text')
    ->
render();
echo
$f->renderRaw('<br>');
echo
$f->id('field2')
    ->
label('field 2:')
    ->
addExtra('style','cursor: pointer;')
    ->
onClick('alert("ok");')
    ->
type('label')->render();
echo
$f->id('field2')
    ->
type('text')
    ->
render();
echo
$f->renderRaw('<br>');
echo
$f->id('field3')
    ->
label('field 3:')
    ->
type('label')
    ->
addExtra('style','cursor: pointer;')
    ->
onClick('alert(\'ok\');')
    ->
render();
echo
$f->id('field3')
    ->
label('field 2:')
    ->
type('select')
    ->
addItem('','--select a field--')
    ->
onChange('alert("changed")')
    ->
bind(['id'=>'id','text'=>'text','extra'=>''])
    ->
addItems(
        [
            [
'id'=>1,'text'=>'America'],
            [
'id'=>2,'text'=>'Asia'],
            [
'id'=>3,'text'=>'Europa'],
        ])
    ->
render();

echo
$f->end();
?>
</html>




Details

FormOne

Creates HTML web Form on PHP

Packagist Total Downloads [Maintenance]() [composer]() [php]() [php]() [CocoaPods]()

Instead of write this code

<form method='POST' enctype='multipart/form-data' >
<label for='field1'>field 1:</label>
<input type='text' name='field1' id='field1'value='' />
<br>
</form>

Use instead this one

<?php
$f=new \eftec\FormOne();

echo $f->start();
echo $f->id('field1')
    ->label('field 1:')
    ->type('label')
    ->render();
echo $f->id('field1')
    ->type('text')
    ->render();
echo $f->renderRaw('<br>');
echo $f->end();

render()

It's the end of the chain. It generates the end result (html)

start()

Start a form (<form>)

end()

End a form (</form>)

idForm($idForm)

It sets the identifier of the current form.

prefix($prefix)

It marks the prefix used by the name fields. Example "frm_"

name($name)

Sets the name of the current chain.

> Note: if id() is not set at the end of the chain then, it also sets the id

id($id)

it sets the id of the current chain.

> Note: if name() is not set at the end of the chain then, it also sets the name

disabled($disabled=true)

It sets the attribute disable of the chain

type($type)

| type | Description | |----------|-------------------------| | select | `<select>` | | text | `<input type='text'>` | | hidden | `<input type='hidden'>` | | password | `<input type='password'>` | | email | `<input type='email'>` | | number | `<input type='number'>` | | checkbox | `<input type='checkbox'>` | | radio | `<input type='radio'>` | | textarea | `<textarea></textarea> ` | | label | `<label>label</label>` | | submit | `<button type='submit'>submit</button>` | | button | `<button type='button'>button</button>` |

addClass($classes)

It adds a class to the current element. You could add many classes using different calls. Examples:

$form
    ->addClass("col-sm-2 col-form-label")

$form
    ->addClass("col-sm-2")
    ->addClass("col-form-label")

classType($type,$classes)

It adds a class to all elements of a type

$f->classType('label','col-sm-2 col-form-label'); // for all labels
$f->classType('text','col-sm-10 form-control'); // for all textbox
$f->classType('select','col-sm-10 form-control'); // for all select


value($value)

It sets the current value, for example the default value of a textbox

itemValue($value)

It sets the value of the element. It's different to value because it's used when the value is "checked"

label($label)

It sets the label of the element. It is used for label,checkbox,radiobuttons and buttons (inner html)

addItem($idOrArray,$text=null,$extra=null)

It adds a simple item to a list. It is commonly used by type="select"

$form->addItem('','--select a field--')

$form->addItem(['id'=>'','text'=>'--select a field--'])

addItems($items)

it adds multiple items to a list.

$array=[
            ['id'=>1,'text'=>'America'],
            ['id'=>2,'text'=>'Asia'],
            ['id'=>3,'text'=>'Europa'],
        ];
$form->addItem($array)

addExtra($type,$value=null)

addAttr($type,$value=null)

onClick($js)

onChange($js)

addJScript($type,$js)

bind($bind)

inner($htmlInner)

readonly($readonly=true)

required($required=true)

Example

$form->type('label')
    ->id('id')
    ->addClass("col-sm-2 col-form-label")
    ->inner('Id 1:')
    ->render()

it renders

<label for='id' class="col-sm-2 col-form-label">Id 1:</label>

version

  • 1.8 2020-01-13 Some small updates
  • 1.7 2018-20-29 A small optimization. Now if the class is empty then it doesn't render class=''
  • 1.6 2018-20-28 Added "hidden" type.
  • 1.5 2018-20-27 Some cleanup and classType()
  • 1.4 2018-20-27 start(),end(),prefix(),idform() and "password" type.
  • 1.2 2018-10-22 Some cleanup.
  • 1.1 2018-10-22 new features
  • 1.0 2018-10-21 first version

License.

Copyright Jorge Castro Castillo Eftec 2018

This program is supplied as dual license, LGPLV2 or commercial.


  Files folder image Files  
File Role Description
Files folder imageexamples (5 files)
Files folder imagelib (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files  /  examples  
File Role Description
  Accessible without login Plain text file common.php Example Example script
  Accessible without login Plain text file firstexample.php Example Example script
  Accessible without login Plain text file firstexamplebootstrap.php Example Example script
  Accessible without login Plain text file html.php Example Example script
  Accessible without login Plain text file htmltable.php Example Example script

  Files folder image Files  /  lib  
File Role Description
  Plain text file FormOne.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:176
This week:1
All time:8,760
This week:571Up