Form::create(string $id, callable $callback, [array $options])
$id
string$callback
callable$options
arrayForm::create('login', function($f){ $f->textField('username', 'Username'); $f->passwordField('password', 'Password'); });
Form::fromArray(string $id, array $array, callable $callback, [array $options])
$id
string$array
array$callback
callable$options
array$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]); });
Form::fromObject(object $obj, callable $callback, [array $options])
$object
object$callback
callable$options
arrayclass 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]); });
The general prototype is:
xyzField($key, $label=null, array $attr=[])
$key
is the name of the data inside the resource object, i.e. array key or member name. If the key isn't found in the resource it is assumed to be ''
.$label
is the textual label describing the field. Setting to false
disables the label. Both null
and ''
are legal values which retains the space for the label but blank.$attr
is an array with optional attributes. Most of these are serialized and passed as-is to the input-field (e.g. passing ['data-foo' => 'bar']
results in <input data-foo="bar">
. Some fields have special options (see table below) which are consumed and not serialized.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
|
hiddenField($key, $value = NULL, array $attr = []) |
Add hidden field. All hiddens are placed at the beginning of the form no matter where used.
Parameters
|
textField($key, $label = NULL, array $attr = []) |
Regular "text" input.
Attribute options
|
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
|
hint($text, $label = NULL, array $attr = []) |
Add a help text. |
manual($key, $label, $content, $hint = false) |
Create a manual field from HTML.
Parameters
|
uploadField($key, $label = NULL, array $attr = []) |
File upload field.
Attribute options
|
group($label, callable $callback, array $attr = []) |
Create a field group where all fields is aligned horizontaly, useful for buttons, checkboxes and radiobuttons.
Parameters
|
fieldset($label, callable $callback) |
Form fieldset.
Parameters
|
submit($text, array $attr = []) |
Submit button.
Attribute options
|
button($text, array $attr = []) |
Generic button.
Attribute options
|
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
|
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
|
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'); });