Form
Form can be any of supported display types, tabbed form or the form itself. You can combine forms with displays as you want.
..->createAndEdit(function ()
{
$form = AdminForm::form();
$form->items([
// create form items here
]);
return $form;
})
Tabbed Form
..->createAndEdit(function ()
{
$form = AdminForm::tabbed();
$form->items([
'Main Tab Label' => [
// form items
],
'Second Tab Label' => [
// form items
],
]);
return $form;
})
Form Items
You can create form item with the following code:
FormItem::{type}('{field name}', '{label}')
Supported Form Items
- columns
- text
- textaddon
- hidden
- checkbox
- date
- time
- timestamp
- file
- image
- images
- select
- multiselect
- radio
- textarea
- ckeditor
- custom
- view
Validation
FormItem::text('title')->required()->unique()->validationRule('my-custom-rule')
See details about validation.
Default Value
You can set default value for form element.
FormItem::text('title')->defaultValue('My new item title')
Register Custom Form Item
You can register your own form elements in bootstrap.php
file within bootstrapDirectory
(default is app/admin/bootstrap.php
).
FormItem::register('{type}', 'App\MyFormItem')
Your class must extend SleepingOwl\Admin\FormItems\BaseFormItem
(if you dont want to get value from your instance by name) or SleepingOwl\Admin\FormItems\NamedFormItem
(in opposite case).
Adding Custom Scripts and Styles
You can add custom scripts and styles to the page header, that uses your custom form element.
public function initialize()
{
parent::initialize();
AssetManager::addScript(asset('my.js'));
AssetManager::addStyle(asset('my.css'));
}
Example
bootstrap.php
FormItem::register('myItem', 'Acme\MyItem')
Acme/MyItem.php
use SleepingOwl\Admin\FormItems\NamedFormItem;
class MyItem extends NamedFormItem
{
public function render()
{
$params = $this->getParams();
// $params will contain 'name', 'label', 'value' and 'instance'
return view('my-form-item-view, $params);
}
}
Usage in model configuration
FormItem::myItem('field')->label('My Label');
Columns
Creates multicolumn form. Use columns()
method to divide form items onto columns.
FormItem::columns()->columns([
[
FormItem::...
],
[
...
],
])
Text
Creates text input.
FormItem::text('title', 'Title')
Textaddon
Creates text input with addon in front or end.
Default placement is before
.
FormItem::textaddon('url', 'Url')->addon('http://my-site.com/')->placement('before')
FormItem::textaddon('price', 'Price')->addon('$')->placement('after')
Hidden
Creates hidden input. Usefull with table display parameters (see details).
FormItem::hidden('field')
Checkbox
Creates checkbox with label.
FormItem::checkbox('active', 'Active')
Date
Creates date input.
FormItem::date('date', 'Date')
Default format is described in config dateFormat
property. You can override it using format($format)
method.
FormItem::date('date', 'Date')->format('d.m.Y')
Time
Creates time input.
FormItem::time('time', 'Time')
Default format is described in config timeFormat
property. You can override it using format($format)
method.
FormItem::time('time', 'Time')->format('H:i')
Display Seconds
FormItem::time('time', 'Time')->format('H:i:s')->seconds(true)
Timestamp
Creates timestamp input.
FormItem::timestamp('timestamp', 'Timestamp')
Default format is described in config datetimeFormat
property. You can override it using format($format)
method.
FormItem::timestamp('timestamp', 'Timestamp')->format('d.m.Y H:i')
Display Seconds
FormItem::timestamp('timestamp', 'Timestamp')->format('d.m.Y H:i:s')->seconds(true)
File
Creates file input. File will be uploaded to imagesUploadDirectory
from config. If you want another place to store your files - handle it by yourself in your model.
FormItem::file('file', 'File')
Image
Creates image input with preview. Image will be uploaded to imagesUploadDirectory
from config. If you want another place to store your images - handle it by yourself in your model.
FormItem::image('photo', 'Photo')
Images
Creates images input with preview. Images will be uploaded to imagesUploadDirectory
from config. If you want another place to store your images - handle it by yourself in your model.
Your model must implement field accessors on this field and return array of image urls and save array as a value.
FormItem::images('photos', 'Photos')
The simplest way to load and store images is text
field with accessors:
public function getPhotosAttribute($value)
{
return preg_split('/,/', $value, -1, PREG_SPLIT_NO_EMPTY);
}
public function setPhotosAttribute($photos)
{
$this->attributes['photos'] = implode(',', $photos);
}
Select
Creates select input.
FormItem::select('category_id', 'Category')
Providing Data
With array:
->options([1 => 'First', 2 => 'Second', 3 => 'Third])
With enum (use array values as keys):
->enum(['First', 'Second', 'Third])
With model:
->model('App\MyModel')->display('title')
Nullable Field
You can mark select to be nullable:
…->nullable()
Multiselect
Creates multiple select input.
FormItem::multiselect('categories', 'Categories')
Providing Data
With array:
->options([1 => 'First', 2 => 'Second', 3 => 'Third])
With enum (use array values as keys):
->enum(['First', 'Second', 'Third])
With class:
->model('App\MyModel')->display('title')
Saving Data
Create new mutator method in your model. Here is an example:
public function setCategoriesAttribute($categories)
{
$this->categories()->detach();
if ( ! $categories) return;
if ( ! $this->exists) $this->save();
$this->categories()->attach($categories);
}
categories()
method creates belongs-to-many
relation in this case.
Radio
Creates radio input.
FormItem::radio('category_id', 'Category')
Providing Data
With array:
->options([1 => 'First', 2 => 'Second', 3 => 'Third])
With enum (use array values as keys):
->enum(['First', 'Second', 'Third])
With model:
->model('App\MyModel')->display('title')
Nullable Field
You can mark radio element to be nullable:
…->nullable()
Textarea
Creates textarea.
FormItem::textarea('text', 'Text')
CKEditor
Creates ckeditor instance.
FormItem::ckeditor('text', 'Text')
Custom
Creates custom form item. You must specify display method (display()
) and save method (callback()
).
FormItem::custom()->display(function ($instance)
{
return view('my-form-item', ['instance' => $instance]);
})->callback(function ($instance)
{
$instance->myField = 12;
})
View
Insert your custom view. You can write there anything you want and insert scripts. View will be rendered with $instance
variable.
FormItem::view('admin.article.view')