Encore\Admin\Widgets\Form
class is used to quickly build a form:
$form = new Form();
$form->action('example');
$form->email('email')->default('[email protected]');
$form->password('password');
$form->text('name');
$form->url('url');
$form->color('color');
$form->map('lat', 'lng');
$form->date('date');
$form->json('val');
$form->dateRange('created_at', 'updated_at');
echo $form->render();
Form::__construct($data = [])
generates a form object. If the $data
parameter is passed, the elements in the $data
array will be filled into the form.
Form::action($uri)
method is used to set the form submission address.
Form::method($method)
method is used to set the submit method of the form form, the default is POST
method.
Form::disablePjax()
disable pjax for form submit.
The Form
object implements nearly 30 form elements via the magic method __call()
, which adds the form element with a short call:
$form->text($column, [$label]);
// Add a submission validation rule
$form->text($column, [$label])->rules('required|min:10');
$form->select($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);
$form->multipleSelect($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);
$form->textarea($column[, $label]);
$form->radio($column[, $label])->values(['m' => 'Female', 'f'=> 'Male'])->default('m');
The values()
method is used to set options:
$form->checkbox($column[, $label])->values([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);
$form->email($column[, $label]);
$form->password($column[, $label]);
$form->url($column[, $label]);
$form->ip($column[, $label]);
$form->mobile($column[, $label])->format('999 9999 9999');
$form->color($column[, $label])->default('#ccc');
$form->time($column[, $label]);
// 设置时间格式,更多格式参考http://momentjs.com/docs/#/displaying/format/
$form->time($column[, $label])->format('HH:mm:ss');
$form->date($column[, $label]);
// 设置日期格式,更多格式参考http://momentjs.com/docs/#/displaying/format/
$form->date($column[, $label])->format('YYYY-MM-DD');
$form->datetime($column[, $label]);
// Set the date format, more format reference http://momentjs.com/docs/#/displaying/format/
$form->datetime($column[, $label])->format('YYYY-MM-DD HH:mm:ss');
$startTime
、$endTime
is the start and end time fields:
$form->timeRange($startTime, $endTime, 'Time Range');
$startDate
、$endDate
is the start and end date fields:
$form->dateRange($startDate, $endDate, 'Date Range');
$startDateTime
、$endDateTime
is the start and end datetime fields:
$form->datetimeRange($startDateTime, $endDateTime, 'DateTime Range');
$form->currency($column[, $label]);
// Sets the unit symbol
$form->currency($column[, $label])->symbol('¥');
$form->number($column[, $label]);
$form->decimal($column[, $label]);
$form->rate($column[, $label]);
You can use compression, crop, add watermarks and other methods, please refer to [Intervention],The image upload directory is configured in upload
in the file config/admin.php
. If the directory does not exist, it needs to be created and write-enabled. :
$form->image($column[, $label]);
// Modify the image upload path and file name
$form->image($column[, $label])->move($dir, $name);
// Crop picture
$form->image($column[, $label])->crop(int $width, int $height, [int $x, int $y]);
// Add a watermark
$form->image($column[, $label])->insert($watermark, 'center');
文件上传目录在文件config/admin.php
中的upload.file
中配置,如果目录不存在,需要创建该目录并开放写权限。
The file upload directory is configured in upload
in the file config/admin.php
. If the directory does not exist, create the directory and open the write permissions.
$form->file($column[, $label]);
// Modify the file upload path and file name
$form->file($column[, $label])->move($dir, $name);
// And set the upload file type
$form->file($column[, $label])->rules('mimes:doc,docx,xlsx');
Map element is used to select the latitude and longitude, $latitude
,$longitude
for the latitude and longitude field, using Tencent map if the locale
in config/app.php
is set to zh_CN
,otherwise use Google Maps:
$form->map($latitude, $longitude, $label);
// Use Tencent map
$form->map($latitude, $longitude, $label)->useTencentMap();
// Use Google Maps
$form->map($latitude, $longitude, $label)->useGoogleMap();
Can be used to select the type of digital fields, such as age:
$form->slider($column[, $label])->options(['max' => 100, 'min' => 1, 'step' => 1, 'postfix' => 'years old']);
$form->editor($column[, $label]);
$form->json($column[, $label]);
Hidden
$form->hidden($column);
on
and off
pairs of switches with the values 1
and 0
:
$form->switch($column[, $label])->states(['on' => 1, 'off' => 0]);
Only the fields are displayed without any action:
$form->display($column[, $label]);
$form->divide();