diff --git a/Jsoneditor.php b/Jsoneditor.php index 9de6eb1..eeb5bb9 100644 --- a/Jsoneditor.php +++ b/Jsoneditor.php @@ -5,6 +5,7 @@ use yii\helpers\BaseInflector; use yii\helpers\Html; use yii\helpers\Json; +use yii\web\JsExpression; use yii\widgets\InputWidget; class Jsoneditor extends InputWidget @@ -41,17 +42,20 @@ public function run() $view = $this->getView(); JsoneditorAsset::register($view); $editorName = BaseInflector::camelize($this->id) . 'Editor'; + // Add possible user defined change function to ActiveField update method + $this->editorOptions['change'] = new JsExpression( + "function() {". + "jQuery('#" . $this->id . "').val(" . $editorName . ".getText());" . + (isset($this->editorOptions['change'])?$this->editorOptions['change']:'') . + "}" + ); $view->registerJs( "var container = document.getElementById('" . $this->options['id'] . "'); - var options = " . Json::encode($this->editorOptions) . "; + var options = " . Json::encode($this->editorOptions). "; var json = " . $this->value . "; - " . $editorName . " = new JSONEditor(container, options, json); - jQuery('#" . $this->id . "').parents('form').eq(0).submit(function() { - jQuery('#" . $this->id . "').val(" . $editorName . ".getText()); - return true; - });" + " . $editorName . " = new JSONEditor(container, options, json);" ); echo Html::hiddenInput($this->name, $this->value, ['id' => $this->id]); echo Html::tag('div', '', $this->options); } -} +} \ No newline at end of file