Field Elements
Methods for rendering individual form fields.
The field element methods are the basic building blocks of your form. Most of them accept the same four parameters:
- name -
string|array(required) - The field name or array configuration - label -
string(optional) - The label text - value -
string(optional) - Default value - attributes -
string|array(optional) - Additional attributes
Two are shaped differently: hidden takes only a name and a value, and
datalist adds its options array as a fourth parameter, pushing
attributes to fifth.
Tip
value is always a string. Passing an array raises a TypeError, including on
selectMultiple.
When the first parameter is an array, the label parameter is ignored — put the
label inside the array instead.
text
Creates a standard text input field.
$form->text('username', 'Username', '', ['required' => true, 'maxlength' => 20]);
email
Creates an email input field.
$form->email('user_email', 'Email Address', '', ['required' => true]);
password
Creates a password input field.
$form->password('password', 'Password');
textarea
Creates a textarea input field.
$form->textarea('description', 'Description', '', ['rows' => 5, 'cols' => 50]);
select
Creates a select dropdown menu.
// With options array $form->select('foo', 'Label', '', [ 'foo' => 'Foo', 'bar' => 'Bar' ]); // Passing a default value $form->select('state', 'States', 'FL', 'states'); // With prebuilt dropdown $form->select('state', 'States', '', 'states');
selectMultiple
Creates a multi-select dropdown menu. The rendered field is named skills[], so
the submission arrives as an array.
$form->selectMultiple('skills', 'Skills', 'php', [ 'options' => [ 'php' => 'PHP', 'js' => 'JavaScript', 'python' => 'Python' ] ]);
The third parameter is a string, so it can preselect one option. It cannot take
an array — that raises a TypeError. After a submission Flick reselects every
option the user picked, from the posted data, without you doing anything.
checkbox
Creates a checkbox input.
$form->checkbox('terms', 'I agree to the terms', 'agreed', ['required' => true]);
checkboxInline
Creates an inline checkbox input.
$form->checkboxInline('remember', 'Remember me', 'yes');
radio
Creates a radio button input.
$form->radio('gender', 'Male', 'male'); $form->radio('gender', 'Female', 'female');
radioInline
Creates an inline radio button input.
$form->radioInline('size', 'Small', 'S'); $form->radioInline('size', 'Medium', 'M'); $form->radioInline('size', 'Large', 'L');
file
Creates a file upload input.
$form->file('avatar', 'Profile Picture', '', ['accept' => 'image/*']);
files
Creates a file input that allows multiple file selections.
$form->files('documents', 'Upload Documents', '', ['accept' => '.pdf,.doc,.docx']);
The rendered field is named documents[] — Flick appends the [] for you,
because PHP only receives more than one file when the input name ends in [].
hidden
Creates a hidden input field.
$form->hidden('user_id', '123');
input
Creates a generic input element. The type can be specified via the attributes array or by passing an array as the first parameter.
// Using attributes to set type $form->input('custom', 'Custom Field', '', ['type' => 'email']); // Using array configuration — put the label in the array, not the second parameter $form->input(['name' => 'custom', 'type' => 'email', 'label' => 'Custom Field'], '');
number
Creates a number input field.
$form->number('quantity', 'Quantity', '1', ['min' => '0', 'max' => '100']);
date
Creates a date input field.
$form->date('birth_date', 'Date of Birth', '', ['min' => '1900-01-01']);
datetime
Creates a datetime-local input field.
$form->datetime('event_time', 'Event Date and Time');
time
Creates a time input field.
$form->time('appointment_time', 'Appointment Time', '', ['min' => '09:00', 'max' => '17:00']);
color
Creates a color picker input field.
$form->color('theme_color', 'Theme Color', '#3366cc');
range
Creates a range slider input field.
$form->range('volume', 'Volume', '50', ['min' => '0', 'max' => '100', 'step' => '1']);
tel
Creates a telephone number input field.
$form->tel('phone', 'Phone Number', '', ['pattern' => '[0-9]{3}-[0-9]{3}-[0-9]{4}']);
url
Creates a URL input field.
$form->url('website', 'Your Website', 'https://');
search
Creates a search input field.
$form->search('query', 'Search', '', ['placeholder' => 'Enter search terms']);
datalist
Creates a text input with autocomplete suggestions using HTML5 datalist. The browser shows matching options as the user types.
// Using the dedicated method $form->datalist('country', 'Country', '', ['USA', 'Canada', 'Mexico']); // With key-value pairs (key is submitted, value is displayed) $form->datalist('country', 'Country', '', ['us' => 'United States', 'ca' => 'Canada']); // Using the datalist attribute on any compatible input $form->text('browser', 'Browser', '', ['datalist' => ['Chrome', 'Firefox', 'Safari']]); $form->search('query', 'Search', '', ['datalist' => ['Popular search 1', 'Popular search 2']]);
month
Creates a month input field.
$form->month('start_month', 'Start Month');
week
Creates a week input field.
$form->week('vacation_week', 'Vacation Week');