add custom messages & callbacks for option forms

This commit is contained in:
printempw 2016-11-18 22:39:12 +08:00
parent a91da6cf3c
commit eb3b963dca
2 changed files with 29 additions and 9 deletions

View File

@ -16,6 +16,10 @@ class OptionForm
protected $success = false; protected $success = false;
protected $messages = [];
protected $alwaysCallback = null;
public function __construct($id, $title) public function __construct($id, $title)
{ {
$this->id = $id; $this->id = $id;
@ -89,11 +93,16 @@ class OptionForm
return $this; return $this;
} }
public function addMessage($msg, $type = "info")
{
$this->messages[] = "<div class='callout callout-$type'>$msg</div>";
}
public function handle($callback = null) public function handle($callback = null)
{ {
if (Arr::get($_POST, 'option') == $this->id) { if (Arr::get($_POST, 'option') == $this->id) {
if (!is_null($callback)) { if (!is_null($callback)) {
call_user_func($callback); call_user_func($callback, $this);
} }
foreach ($this->items as $item) { foreach ($this->items as $item) {
@ -116,8 +125,19 @@ class OptionForm
return $this; return $this;
} }
public function always($callback)
{
$this->alwaysCallback = $callback;
return $this;
}
public function render() public function render()
{ {
if (!is_null($this->alwaysCallback)) {
call_user_func($this->alwaysCallback, $this);
}
foreach ($this->items as $item) { foreach ($this->items as $item) {
$id = $item->id; $id = $item->id;
@ -156,14 +176,7 @@ class OptionForm
$item->setContent($view->render()); $item->setContent($view->render());
} }
return view('vendor.option-form.main')->with(get_object_vars($this))->render();
return view('vendor.option-form.main')->with([
'title' => $this->title,
'id' => $this->id,
'hint' => $this->hint,
'items' => $this->items,
'success' => $this->success
])->render();
} }
} }

View File

@ -8,6 +8,13 @@
@if ($success) @if ($success)
<div class="callout callout-success">设置已保存。</div> <div class="callout callout-success">设置已保存。</div>
@endif @endif
@if (!empty($messages))
@foreach($messages as $msg)
{!! $msg !!}
@endforeach
@endif
<table class="table"> <table class="table">
<tbody> <tbody>
@foreach($items as $item) @foreach($items as $item)