Skip to content

Latest commit

 

History

History
242 lines (185 loc) · 5.68 KB

form.md

File metadata and controls

242 lines (185 loc) · 5.68 KB

Form

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 elements

The Form object implements nearly 30 form elements via the magic method __call(), which adds the form element with a short call:

Text input

$form->text($column, [$label]);

// Add a submission validation rule
$form->text($column, [$label])->rules('required|min:10');

Select

$form->select($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

Multiple select

$form->multipleSelect($column[, $label])->options([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

Textarea

$form->textarea($column[, $label]);

Radio

$form->radio($column[, $label])->values(['m' => 'Female', 'f'=> 'Male'])->default('m');

Checkbox

The values() method is used to set options:

$form->checkbox($column[, $label])->values([1 => 'foo', 2 => 'bar', 'val' => 'Option name']);

Email

$form->email($column[, $label]);

Password

$form->password($column[, $label]);

Url

$form->url($column[, $label]);

Ip

$form->ip($column[, $label]);

Mobile

$form->mobile($column[, $label])->format('999 9999 9999');

Color

$form->color($column[, $label])->default('#ccc');

Time

$form->time($column[, $label]);

// 设置时间格式,更多格式参考http://momentjs.com/docs/#/displaying/format/
$form->time($column[, $label])->format('HH:mm:ss');

Date

$form->date($column[, $label]);

// 设置日期格式,更多格式参考http://momentjs.com/docs/#/displaying/format/
$form->date($column[, $label])->format('YYYY-MM-DD');

Datetime

$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');

Time range

$startTime$endTime is the start and end time fields:

$form->timeRange($startTime, $endTime, 'Time Range');

Date range

$startDate$endDate is the start and end date fields:

$form->dateRange($startDate, $endDate, 'Date Range');

Datetime range

$startDateTime$endDateTimeis the start and end datetime fields:

$form->datetimeRange($startDateTime, $endDateTime, 'DateTime Range');

Currency

$form->currency($column[, $label]);

// Sets the unit symbol
$form->currency($column[, $label])->symbol('');

Number

$form->number($column[, $label]);

Decimal

$form->decimal($column[, $label]);

Rate

$form->rate($column[, $label]);

Image upload

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');

File upload

文件上传目录在文件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

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();

Slider

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']);

Editor

$form->editor($column[, $label]);

Json Editor

$form->json($column[, $label]);

Hidden

$form->hidden($column);

Switch

on and off pairs of switches with the values 1 and 0:

$form->switch($column[, $label])->states(['on' => 1, 'off' => 0]);

Display

Only the fields are displayed without any action:

$form->display($column[, $label]);

Divide line

$form->divide();