Skip to content

Instantly share code, notes, and snippets.

@sleeping-owl
Created April 3, 2015 10:44
Show Gist options
  • Save sleeping-owl/86e0a4be8de0715ac707 to your computer and use it in GitHub Desktop.
Save sleeping-owl/86e0a4be8de0715ac707 to your computer and use it in GitHub Desktop.
Custom form item demo
# admin/bootstrap.php
FormItem::register('autocomplete', App\Misc\FormItems\AutocompleteFormItem::class);
# app/Misc/FormItems/AutocompleteFormItem.php
<?php namespace App\Misc\FormItems;
use AssetManager;
use SleepingOwl\Admin\Models\Form\FormItem\Text;
use SleepingOwl\Admin\Models\Form\Interfaces\FormItemInterface;
use URL;
class AutocompleteFormItem extends Text implements FormItemInterface
{
protected $type;
/**
* @return string
*/
public function render()
{
# add custom javascript
AssetManager::addScript(URL::asset('assets/admin/js/jquery.autocomplete.js'));
AssetManager::addScript(URL::asset('assets/admin/js/autocomplete.js'));
# add custom css
AssetManager::addStyle(URL::asset('assets/admin/css/permissions.css'));
$content = '';
# main element, will be used to send the selected value
$content .= $this->formBuilder->hidden($this->name, $this->getValueFromForm());
# text element for autocomplete
$content .= $this->formBuilder->textGroup('__no_name', $this->label, $this->getValueFromForm(), [
'class' => 'autocomplete',
'data-type' => $this->type,
'data-for' => $this->name,
]);
return $content;
}
public function type($type)
{
$this->type = $type;
return $this;
}
public function name($name)
{
$this->name = $name;
return $this;
}
public function label($label)
{
$this->label = $label;
return $this;
}
}
# usage in model configuration
FormItem::autocomplete()->name('aftermarket_id')->label('Aftermarket')->type('model');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment