dmitry-kulikov / yii2-json-editor Goto Github PK
View Code? Open in Web Editor NEWJSON editor widget (josdejong/jsoneditor) for Yii 2.
License: MIT License
JSON editor widget (josdejong/jsoneditor) for Yii 2.
License: MIT License
$model->data = '"foo"';
echo $form->field($model, 'data', ['inputOptions' => ['value' => '"bar"']])
->widget(JsonEditor::class);
Widget should show:
"bar"
Widget shows:
"foo"
Q | A |
---|---|
Extension version | 2.2.0 |
Yii version | All |
JSON editor version | All |
PHP version | All |
HHVM version | All |
Operating system | All |
$model->data = null;
echo $form->field(
$model,
'data',
['inputOptions' => ['id' => 'json-editor-hidden-input']]
)
->widget(
JsonEditor::class,
[
'defaultValue' => '{"foo": "bar"}',
]
);
In browser console execute:
$('#json-editor-hidden-input').val();
In browser console should be:
"{\"foo\": \"bar\"}"
In browser console:
""
Normally such JS should not be used to access JSON editor, this code just shows problem.
Correct way to access JSON editor is:
var jsonEditor = window[$('#json-editor-hidden-input').data('json-editor-name')];
jsonEditor.get();
It works properly:
Object { foo: "bar" }
Q | A |
---|---|
Extension version | 2.2.0 |
Yii version | All |
JSON editor version | All |
PHP version | All |
HHVM version | All |
Operating system | All |
use kdn\yii2\JsonEditor;
$form = ActiveForm::begin();
echo $form->field($model, '[1]data[1]')->widget(JsonEditor::className());
ActiveForm::end();
Widget displays JSON.
Exception thrown:
2017-09-28 04:07:48 [127.0.0.1][4][4api8mtn0639foed1qttpb48l0][error][yii\base\UnknownPropertyException] exception 'yii\base\UnknownPropertyException' with message 'Setting unknown property: common\models\Test::[1]data[1]' in /path/to/vendor/yiisoft/yii2/base/Component.php:201
Q | A |
---|---|
Extension version | All |
Yii version | All |
JSON editor version | All |
PHP version | All |
HHVM version | All |
Operating system | All |
$default = '{"foo": "bar"}';
echo JsonEditor::widget(['name' => 'test-1', 'value' => '0', 'defaultValue' => $default]);
echo JsonEditor::widget(['name' => 'test-2', 'value' => 'null', 'defaultValue' => $default]);
echo JsonEditor::widget(['name' => 'test-3', 'value' => '""', 'defaultValue' => $default]);
echo JsonEditor::widget(['name' => 'test-4', 'value' => '', 'defaultValue' => $default]);
Widgets should show:
0
null
""
{
"foo": "bar"
}
Widgets show:
{
"foo": "bar"
}
{}
{}
{
"foo": "bar"
}
Q | A |
---|---|
Extension version | 2.2.0 |
Yii version | All |
JSON editor version | All |
PHP version | All |
HHVM version | All |
Operating system | All |
composer install
should install dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for kdn/yii2-json-editor 1.0.2 -> satisfiable by kdn/yii2-json-editor[1.0.2].
- kdn/yii2-json-editor 1.0.2 requires npm-asset/jsoneditor 5.* -> no matching package found.
Q | A |
---|---|
Extension version | 1.0.2 |
Yii version | yii2-app-advanced |
JSON editor version | 1.0.2 |
PHP version | 5.6.30 |
HHVM version | |
Operating system | Ubuntu |
<?= $form->field($model, 'test'
->widget(JsonEditor::class, [
'clientOptions' => ['modes' => ['code', 'tree']],
]);
?>
$model->test will contains array, which is a default behavior when retrieved JSON data type from db, yii do something like
$placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
widget can read data array from JSON data type
Error: Invalid JSON data.
Please check at JsonEditor.php
Line: 170
$encodedValue = Json::htmlEncode(Json::decode($this->value, false));
Maybe we can add a value checking first before decode the value:
$encodedValue => is_array($this->value) ? Json::htmlEncode($this->value) : Json::htmlEncode(Json::decode($this->value, false));
Q | A |
---|---|
Extension version | 2.2.0 |
Yii version | 2.0.27 |
JSON editor version | |
PHP version | 5.3 |
HHVM version | |
Operating system |
$model->data = '"foo"';
echo $form->field($model, 'data', ['inputOptions' => ['value' => '"bar"']])
->widget(JsonEditor::class, ['value' => '"test"']);
Widget should show:
"test"
Widget shows:
"foo"
Q | A |
---|---|
Extension version | 2.2.0 |
Yii version | All |
JSON editor version | All |
PHP version | All |
HHVM version | All |
Operating system | All |
use kdn\yii2\JsonEditor;
echo JsonEditor::widget(
[
'name' => 'editor',
'value' => '{"script": "<script type=\"text/javascript\">alert(\"XSS\");</script>"}',
]
);
Widget displays JSON.
Widget not shown. Error in browser console:
SyntaxError: unterminated string literal
Q | A |
---|---|
Extension version | All |
Yii version | All |
JSON editor version | All |
PHP version | All |
HHVM version | All |
Operating system | All |
Looks like <script>
tag is not properly escaped. This bug may produce risk of XSS vulnerability.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.