PHP Forms Usage

Simple form (unbound)

Form::create(string $id, callable $callback, [array $options])
$id string
Form id (prefix).
$callback callable
Build callback.
$options array
Optional options and form attributes, see Form Options

Example

Form::create('login', function($f){
	$f->textField('username', 'Username');
	$f->passwordField('password', 'Password');
});

Array binding

Form::fromArray(string $id, array $array, callable $callback, [array $options])
$id string
Form id (prefix).
$array array
Array to bind data from.
$callback callable
Build callback.
$options array
Optional options and form attributes, see Form Options

Example

$data = [
	'name' => 'Fred Flintstone',
	'age' => 31,
];
 
Form::fromArray("User", $data, function($f){
	$f->textField('name', 'Name', ['required' => true]);
	$f->textField('age', 'Age', ['type' => 'number', 'min' => 1, 'required' => true]);
});

Object binding

Form::fromObject(object $obj, callable $callback, [array $options])
$object object
Object to bind data from.
$callback callable
Build callback.
$options array
Optional options and form attributes, see Form Options

Example

class User {
	public $name;
	public $age;
};
 
$user = new User;
$user->name = 'Fred Flintstone',
$user->age => 31;
 
Form::fromObject($user, function($f){
	$f->textField('name', 'Name', ['required' => true]);
	$f->textField('age', 'Age', ['type' => 'number', 'min' => 1, 'required' => true]);
});

Fields

The general prototype is:

xyzField($key, $label=null, array $attr=[])

Available fields

Field Description
factory($type, $key, $label = NULL, array $attr = [])
Generates any kind of input, used by most other fields. Note that this function should not be called directly, use customField() instead. Common options for all fields: Attribute options
  • value string If value is set as an attribute it forces the value (irregardless of the object).
  • required boolean Set field to required (using HTML required attribute).
  • prefix string Add a prefix addon.
  • suffix string Add a suffix addon.
hiddenField($key, $value = NULL, array $attr = [])
Add hidden field. All hiddens are placed at the beginning of the form no matter where used. Parameters
  • $value If set the value is used instead of reading from the resource.
textField($key, $label = NULL, array $attr = [])
Regular "text" input. Attribute options
  • type string HTML type attribute, e.g. email or tel.
passwordField($key, $label = NULL, array $attr = [])
Password field.
customField($key, $type, $label = NULL, array $attr = [])
Wrapper around factory.
select($key, $label, $options, array $attr = [])
Select (dropdown) field. Used in conjunction with FormOptions. Parameters
  • $options Instance of FormOptions.
Attribute options
  • postback boolean Automatically submit form when value changes. (default: false)
hint($text, $label = NULL, array $attr = [])
Add a help text.
manual($key, $label, $content, $hint = false)
Create a manual field from HTML. Parameters
  • $content Any HTML.
uploadField($key, $label = NULL, array $attr = [])
File upload field. Attribute options
  • remove boolean If true a checkbox to remove the current value will be added.
  • current html If set to non-false the content will be displayed as the current value, e.g can be set to <img ..> to display the current uploaded image.
group($label, callable $callback, array $attr = [])
Create a field group where all fields is aligned horizontaly, useful for buttons, checkboxes and radiobuttons. Parameters
  • $callback A new rendering context.
fieldset($label, callable $callback)
Form fieldset. Parameters
  • $callback A new rendering context.
submit($text, array $attr = [])
Submit button. Attribute options
  • confirm string Adds a javascript confirmation prompt before submit/click: onclick="return confirm(...);"
button($text, array $attr = [])
Generic button. Attribute options
  • type string Should be a valid HTML button value (e.g. submit or button).
staticValue($key, $label = false, array $attr = [])
Display a value from the resource but provides no editable field.
link($text, $href, $label = false, array $attr = [])
Similar to static but provides a link as well.
textarea($key, $label = NULL, array $attr = [])
Create textarea. Attribute options
  • tworow boolean Layout hint to use two rows having the label on one row label and the textfield below.
  • fill boolean Layout hint used together with 'tworow' to fill the entire row (not just label + content).
checkbox($key, $text, $label = NULL, array $attr = [])
Checkbox field.
fieldsFor($id, $obj, callable $callback)
Changes the resource object to another object. Used to generate forms for multiple object at the same times. Objects doesn't have to be of the same type but ID must be unique. Parameters
  • $callback A new rendering context.

Extending fields

Custom fields or customization of existing fields is possible by extending FormBuilder.

class MyBuilder extends FormBuilder {
	public function myField($label){
		$this->manual(false, $label, 'custom');
	}
}
 
class MyForm extends Form {
	public static $defaultBuilder = MyBuilder::class;
}
 
MyForm::create("id", function($f){
	$f->myField('Label');
});
custom
Fork me on GitHub