Created
September 30, 2016 04:09
-
-
Save rkgrep/6d3a987279c2d6009cfbb05c43251527 to your computer and use it in GitHub Desktop.
Laravel Simple Forms
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
/** | |
* Generate a URL to a controller action. | |
* | |
* @param string $name | |
* @param array $parameters | |
* @param bool $absolute | |
* @return string | |
*/ | |
public function action($name, $parameters = [], $absolute = true) | |
{ | |
if (strpos($name, '@') == false) { | |
$name = static::class.'@'.ltrim($name, '@'); | |
} | |
return action($name, $parameters, $absolute); | |
} | |
//... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form class="form-horizontal" role="form" method="POST" action="{{ $form->url() }}"> | |
{{ csrf_field() }} | |
{{ method_field($form->method()) }} | |
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}"> | |
<label for="name" class="col-md-4 control-label">{{ trans('validation.attributes.name') }}</label> | |
<div class="col-md-6"> | |
<input id="name" type="text" class="form-control" name="name" value="{{ $form->value('name') }}"> | |
@if ($errors->has('name')) | |
<span class="help-block"> | |
<strong>{{ $errors->first('name') }}</strong> | |
</span> | |
@endif | |
</div> | |
</div> | |
<div class="form-group"> | |
<label for="login" class="col-md-4 control-label">{{ trans('validation.attributes.login') }}</label> | |
<div class="col-md-6"> | |
<p class="form-control-static" id="login">{{ $form->value('login') }}</p> | |
</div> | |
</div> | |
<div class="form-group"> | |
<div class="col-md-6 col-md-offset-4"> | |
<button type="submit" class="btn btn-primary"> | |
<i class="fa fa-btn fa-save"></i> {{ trans('layout.buttons.save') }} | |
</button> | |
<a href="{{ url()->previous() }}" class="btn btn-default">{{ trans('layout.buttons.back') }}</a> | |
</div> | |
</div> | |
</form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TS\Backend\UX; | |
use Illuminate\Database\Eloquent\Model; | |
use Illuminate\Session\Store; | |
use Illuminate\Support\Arr; | |
use Illuminate\Support\Fluent; | |
class Form extends Fluent | |
{ | |
/** | |
* @var \Illuminate\Session\Store | |
*/ | |
protected $session = null; | |
public function __construct($attributes) | |
{ | |
parent::__construct($attributes); | |
$this->session = app(Store::class); | |
} | |
public function url() | |
{ | |
return $this->get('url'); | |
} | |
public function method() | |
{ | |
return $this->get('method', 'POST'); | |
} | |
public function data(array $data = null) | |
{ | |
if (is_null($data)) { | |
return $this->get('data', []); | |
} | |
$this->attributes['data'] = $data; | |
return $this; | |
} | |
/** | |
* @param Model|null $model | |
* @return $this|Model|null | |
*/ | |
public function model(Model $model = null) | |
{ | |
if (is_null($model)) { | |
return $this->get('model'); | |
} | |
$this->attributes['model'] = $model; | |
return $this; | |
} | |
public function value($key, $default = null) | |
{ | |
if ($this->session->hasOldInput($key)) { | |
return $this->session->getOldInput($key); | |
} | |
if ($model = $this->model()) { | |
return data_get($model, $key); | |
} | |
return Arr::get($this->data(), $key); | |
} | |
/** | |
* Transform key from array to dot syntax. | |
* | |
* @param string $key | |
* @return string | |
*/ | |
protected function transformKey($key) | |
{ | |
return str_replace(array('.', '[]', '[', ']'), array('_', '', '.', ''), $key); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//... | |
public function edit(Device $device) | |
{ | |
$this->authorize('edit', $device); | |
$data = [ | |
'name' => $device->name, | |
'login' => $device->identifier->login, | |
]; | |
$form = (new Form([ | |
'url' => $this->action('update', [$device->getRouteKey()]), | |
'method' => 'PUT', | |
]))->data($data); | |
return view('devices.form', [ | |
'form' => $form, | |
]); | |
} | |
//... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment