Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b493269d5 | ||
|
|
14e0e0130a | ||
|
|
5dc2d138b8 |
No files matched your search
@@ -41,10 +41,41 @@ trait FormAccessible
|
|||||||
return $this->mutateFormAttribute($key, $value);
|
return $this->mutateFormAttribute($key, $value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$keys = explode('.', $key);
|
||||||
|
|
||||||
|
if ($this->isNestedModel($keys[0])) {
|
||||||
|
$relatedModel = $this->getRelation($keys[0]);
|
||||||
|
|
||||||
|
unset($keys[0]);
|
||||||
|
$key = implode('.', $keys);
|
||||||
|
|
||||||
|
if ($this->hasFormMutator($key)) {
|
||||||
|
return $relatedModel->getFormValue($key);
|
||||||
|
}
|
||||||
|
|
||||||
|
return data_get($relatedModel, $key);
|
||||||
|
}
|
||||||
|
|
||||||
// No form mutator, let the model resolve this
|
// No form mutator, let the model resolve this
|
||||||
return data_get($this, $key);
|
return data_get($this, $key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Check for a nested model.
|
||||||
|
*
|
||||||
|
* @param string $key
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
*/
|
||||||
|
public function isNestedModel($key)
|
||||||
|
{
|
||||||
|
if (in_array($key, array_keys($this->getRelations()))) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $key
|
* @param $key
|
||||||
*
|
*
|
||||||
|
|||||||
+47
-22
@@ -90,6 +90,14 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio'];
|
protected $skipValueTypes = ['file', 'password', 'checkbox', 'radio'];
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Input Type.
|
||||||
|
*
|
||||||
|
* @var null
|
||||||
|
*/
|
||||||
|
protected $type = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new form builder instance.
|
* Create a new form builder instance.
|
||||||
*
|
*
|
||||||
@@ -255,6 +263,8 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
public function input($type, $name, $value = null, $options = [])
|
public function input($type, $name, $value = null, $options = [])
|
||||||
{
|
{
|
||||||
|
$this->type = $type;
|
||||||
|
|
||||||
if (! isset($options['name'])) {
|
if (! isset($options['name'])) {
|
||||||
$options['name'] = $name;
|
$options['name'] = $name;
|
||||||
}
|
}
|
||||||
@@ -467,6 +477,8 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
public function textarea($name, $value = null, $options = [])
|
public function textarea($name, $value = null, $options = [])
|
||||||
{
|
{
|
||||||
|
$this->type = 'textarea';
|
||||||
|
|
||||||
if (! isset($options['name'])) {
|
if (! isset($options['name'])) {
|
||||||
$options['name'] = $name;
|
$options['name'] = $name;
|
||||||
}
|
}
|
||||||
@@ -545,6 +557,8 @@ class FormBuilder
|
|||||||
array $selectAttributes = [],
|
array $selectAttributes = [],
|
||||||
array $optionsAttributes = []
|
array $optionsAttributes = []
|
||||||
) {
|
) {
|
||||||
|
$this->type = 'select';
|
||||||
|
|
||||||
// When building a select box the "value" attribute is really the selected one
|
// When building a select box the "value" attribute is really the selected one
|
||||||
// so we will use that when checking the model or session for a value which
|
// so we will use that when checking the model or session for a value which
|
||||||
// should provide a convenient method of re-populating the forms on post.
|
// should provide a convenient method of re-populating the forms on post.
|
||||||
@@ -777,6 +791,8 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
protected function checkable($type, $name, $value, $checked, $options)
|
protected function checkable($type, $name, $value, $checked, $options)
|
||||||
{
|
{
|
||||||
|
$this->type = $type;
|
||||||
|
|
||||||
$checked = $this->getCheckedState($type, $name, $value, $checked);
|
$checked = $this->getCheckedState($type, $name, $value, $checked);
|
||||||
|
|
||||||
if ($checked) {
|
if ($checked) {
|
||||||
@@ -1096,15 +1112,21 @@ class FormBuilder
|
|||||||
return $value;
|
return $value;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (! is_null($this->old($name)) && $name != '_method') {
|
$old = $this->old($name);
|
||||||
return $this->old($name);
|
|
||||||
|
if (! is_null($old) && $name != '_method') {
|
||||||
|
return $old;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (function_exists('app')) {
|
if (function_exists('app')) {
|
||||||
$hasNullMiddleware = app("Illuminate\Contracts\Http\Kernel")
|
$hasNullMiddleware = app("Illuminate\Contracts\Http\Kernel")
|
||||||
->hasMiddleware(ConvertEmptyStringsToNull::class);
|
->hasMiddleware(ConvertEmptyStringsToNull::class);
|
||||||
|
|
||||||
if ($hasNullMiddleware && is_null($this->old($name)) && $this->view->shared('errors')->count() > 0) {
|
if ($hasNullMiddleware
|
||||||
|
&& is_null($old)
|
||||||
|
&& !is_null($this->view->shared('errors'))
|
||||||
|
&& count($this->view->shared('errors')) > 0
|
||||||
|
) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1128,28 +1150,13 @@ class FormBuilder
|
|||||||
*/
|
*/
|
||||||
protected function getModelValueAttribute($name, $model = null)
|
protected function getModelValueAttribute($name, $model = null)
|
||||||
{
|
{
|
||||||
if (!isset($model)) {
|
|
||||||
$model = $this->model;
|
|
||||||
}
|
|
||||||
|
|
||||||
$key = $this->transformKey($name);
|
$key = $this->transformKey($name);
|
||||||
|
|
||||||
// Check for nested attribute
|
if (method_exists($this->model, 'getFormValue')) {
|
||||||
if (strpos($key, '.') !== false) {
|
return $this->model->getFormValue($key);
|
||||||
$keys = explode('.', $key, 2);
|
|
||||||
|
|
||||||
// get the first level
|
|
||||||
$_model = $this->getModelValueAttribute($keys[0], $model);
|
|
||||||
|
|
||||||
// recursively get the next levels
|
|
||||||
return $this->getModelValueAttribute($keys[1], $_model);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (method_exists($model, 'getFormValue')) {
|
return data_get($this->model, $this->transformKey($name));
|
||||||
return $model->getFormValue($key);
|
|
||||||
}
|
|
||||||
|
|
||||||
return data_get($model, $key);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1162,7 +1169,25 @@ class FormBuilder
|
|||||||
public function old($name)
|
public function old($name)
|
||||||
{
|
{
|
||||||
if (isset($this->session)) {
|
if (isset($this->session)) {
|
||||||
return $this->session->getOldInput($this->transformKey($name));
|
$key = $this->transformKey($name);
|
||||||
|
$payload = $this->session->getOldInput($key);
|
||||||
|
|
||||||
|
if (!is_array($payload)) {
|
||||||
|
return $payload;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!in_array($this->type, ['select', 'checkbox'])) {
|
||||||
|
if (!isset($this->payload[$key])) {
|
||||||
|
$this->payload[$key] = collect($payload);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!empty($this->payload[$key])) {
|
||||||
|
$value = $this->payload[$key]->shift();
|
||||||
|
return $value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $payload;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+21
-15
@@ -5,6 +5,7 @@ namespace Collective\Html;
|
|||||||
use Illuminate\Support\Facades\Blade;
|
use Illuminate\Support\Facades\Blade;
|
||||||
use Illuminate\Support\ServiceProvider;
|
use Illuminate\Support\ServiceProvider;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Illuminate\View\Compilers\BladeCompiler;
|
||||||
|
|
||||||
class HtmlServiceProvider extends ServiceProvider
|
class HtmlServiceProvider extends ServiceProvider
|
||||||
{
|
{
|
||||||
@@ -37,8 +38,7 @@ class HtmlServiceProvider extends ServiceProvider
|
|||||||
$this->app->alias('html', HtmlBuilder::class);
|
$this->app->alias('html', HtmlBuilder::class);
|
||||||
$this->app->alias('form', FormBuilder::class);
|
$this->app->alias('form', FormBuilder::class);
|
||||||
|
|
||||||
$this->setBladeDirectives('Html', get_class_methods(HtmlBuilder::class));
|
$this->registerBladeDirectives();
|
||||||
$this->setBladeDirectives('Form', get_class_methods(FormBuilder::class));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -68,25 +68,31 @@ class HtmlServiceProvider extends ServiceProvider
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set blade directives
|
* Register Blade directives.
|
||||||
*
|
|
||||||
* @param string $namespace
|
|
||||||
* @param array $methods
|
|
||||||
*
|
*
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function setBladeDirectives($namespace, $methods)
|
protected function registerBladeDirectives()
|
||||||
{
|
{
|
||||||
foreach ($methods as $method) {
|
$this->app->afterResolving('blade.compiler', function (BladeCompiler $bladeCompiler) {
|
||||||
if (in_array($method, $this->directives)) {
|
$namespaces = [
|
||||||
$snakeMethod = Str::snake($method);
|
'Html' => get_class_methods(HtmlBuilder::class),
|
||||||
$directive = strtolower($namespace).'_'.$snakeMethod;
|
'Form' => get_class_methods(FormBuilder::class),
|
||||||
|
];
|
||||||
|
|
||||||
Blade::directive($directive, function ($expression) use ($namespace, $method) {
|
foreach ($namespaces as $namespace => $methods) {
|
||||||
return "<?php echo $namespace::$method($expression); ?>";
|
foreach ($methods as $method) {
|
||||||
});
|
if (in_array($method, $this->directives)) {
|
||||||
|
$snakeMethod = Str::snake($method);
|
||||||
|
$directive = strtolower($namespace).'_'.$snakeMethod;
|
||||||
|
|
||||||
|
$bladeCompiler->directive($directive, function ($expression) use ($namespace, $method) {
|
||||||
|
return "<?php echo $namespace::$method($expression); ?>";
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -50,11 +50,15 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase
|
|||||||
{
|
{
|
||||||
$model = new ModelThatUsesForms($this->modelData);
|
$model = new ModelThatUsesForms($this->modelData);
|
||||||
$relatedModel = new ModelThatUsesForms($this->modelData);
|
$relatedModel = new ModelThatUsesForms($this->modelData);
|
||||||
|
$relatedModel->address = [
|
||||||
|
'street' => '123 Evergreen Terrace'
|
||||||
|
];
|
||||||
$model->setRelation('related', $relatedModel);
|
$model->setRelation('related', $relatedModel);
|
||||||
|
|
||||||
$this->formBuilder->setModel($model);
|
$this->formBuilder->setModel($model);
|
||||||
|
|
||||||
$this->assertEquals($this->formBuilder->getValueAttribute('related[string]'), 'ponmlkjihgfedcba');
|
$this->assertEquals($this->formBuilder->getValueAttribute('related[string]'), 'ponmlkjihgfedcba');
|
||||||
$this->assertEquals($this->formBuilder->getValueAttribute('related[created_at]'), $this->now->timestamp);
|
$this->assertEquals($this->formBuilder->getValueAttribute('related[address][street]'), '123 Evergreen Terrace');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testItCanGetRelatedValueForForms()
|
public function testItCanGetRelatedValueForForms()
|
||||||
|
|||||||
@@ -112,12 +112,21 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->assertEquals('<input class="span2" name="foo" type="text">', $form4);
|
$this->assertEquals('<input class="span2" name="foo" type="text">', $form4);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testFormTextArray()
|
||||||
|
{
|
||||||
|
$form1 = $this->formBuilder->input('text', 'foo[]', 'testing');
|
||||||
|
$form2 = $this->formBuilder->text('foo[]');
|
||||||
|
|
||||||
|
$this->assertEquals('<input name="foo[]" type="text" value="testing">', $form1);
|
||||||
|
$this->assertEquals('<input name="foo[]" type="text">', $form2);
|
||||||
|
}
|
||||||
|
|
||||||
public function testFormTextRepopulation()
|
public function testFormTextRepopulation()
|
||||||
{
|
{
|
||||||
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));
|
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));
|
||||||
$this->setModel($model = ['relation' => ['key' => 'attribute'], 'other' => 'val']);
|
$this->setModel($model = ['relation' => ['key' => 'attribute'], 'other' => 'val']);
|
||||||
|
|
||||||
$session->shouldReceive('getOldInput')->twice()->with('name_with_dots')->andReturn('some value');
|
$session->shouldReceive('getOldInput')->once()->with('name_with_dots')->andReturn('some value');
|
||||||
$input = $this->formBuilder->text('name.with.dots', 'default value');
|
$input = $this->formBuilder->text('name.with.dots', 'default value');
|
||||||
$this->assertEquals('<input name="name.with.dots" type="text" value="some value">', $input);
|
$this->assertEquals('<input name="name.with.dots" type="text" value="some value">', $input);
|
||||||
|
|
||||||
@@ -348,12 +357,12 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase
|
|||||||
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));
|
$this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session'));
|
||||||
$this->setModel($model = ['size' => ['key' => 'S'], 'other' => 'val']);
|
$this->setModel($model = ['size' => ['key' => 'S'], 'other' => 'val']);
|
||||||
|
|
||||||
$session->shouldReceive('getOldInput')->twice()->with('size')->andReturn('M');
|
$session->shouldReceive('getOldInput')->once()->with('size')->andReturn('M');
|
||||||
$select = $this->formBuilder->select('size', $list, 'S');
|
$select = $this->formBuilder->select('size', $list, 'S');
|
||||||
$this->assertEquals($select,
|
$this->assertEquals($select,
|
||||||
'<select name="size"><option value="L">Large</option><option value="M" selected="selected">Medium</option><option value="S">Small</option></select>');
|
'<select name="size"><option value="L">Large</option><option value="M" selected="selected">Medium</option><option value="S">Small</option></select>');
|
||||||
|
// its likely that the mock has been changed so its not replicating correctly anymore
|
||||||
$session->shouldReceive('getOldInput')->twice()->with('size.multi')->andReturn(['L', 'S']);
|
$session->shouldReceive('getOldInput')->once()->with('size.multi')->andReturn(['L', 'S']);
|
||||||
$select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']);
|
$select = $this->formBuilder->select('size[multi][]', $list, 'M', ['multiple' => 'multiple']);
|
||||||
$this->assertEquals($select,
|
$this->assertEquals($select,
|
||||||
'<select multiple="multiple" name="size[multi][]"><option value="L" selected="selected">Large</option><option value="M">Medium</option><option value="S" selected="selected">Small</option></select>');
|
'<select multiple="multiple" name="size[multi][]"><option value="L" selected="selected">Large</option><option value="M">Medium</option><option value="S" selected="selected">Small</option></select>');
|
||||||
|
|||||||
Reference in new issue
Block a user