Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

as widget for active form #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions Jsoneditor.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class Jsoneditor extends InputWidget
public function init()
{
parent::init();

if (!isset($this->options['id'])) {
$this->options['id'] = $this->id;
} else {
Expand All @@ -26,21 +27,26 @@ public function init()
if (!isset($this->options['style'])) {
$this->options['style'] = 'height: 250px;';
}
if (is_object($this->model)) {

if ($this->hasModel()) {
$this->value = $this->model->{$this->attribute};
$this->name = Html::getInputName($this->model, $this->attribute);
}

if (empty($this->value)) {
$this->value = '{}';
}

$this->options['id'] .= '-jsoneditor';
}

public function run()
{
$view = $this->getView();
JsoneditorAsset::register($view);

$editorName = BaseInflector::camelize($this->id) . 'Editor';

$view->registerJs(
"var container = document.getElementById('" . $this->options['id'] . "');
var options = " . Json::encode($this->editorOptions) . ";
Expand All @@ -51,7 +57,13 @@ public function run()
return true;
});"
);
echo Html::hiddenInput($this->name, $this->value, ['id' => $this->id]);

if ($this->hasModel()) {
echo Html::activeHiddenInput($this->model, $this->attribute, ['id' => $this->id]);
} else {
echo Html::hiddenInput($this->name, $this->value, ['id' => $this->id]);
}

echo Html::tag('div', '', $this->options);
}
}
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,19 @@ Jsoneditor::widget(
]
)
```

or with active form

```php
use devgroup\jsoneditor\Jsoneditor;

$form->field($model,'name')->widget(Jsoneditor::className(),
[
'editorOptions' => [
'modes' => ['code', 'form', 'text', 'tree', 'view'], // available modes
'mode' => 'tree', // current mode
],
]
);
```