Skip to content
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"zhuravljov/yii2-datetime-widgets" : "^1.1.0",
"dmstr/yii2-ajax-button": "^1.0",
"trntv/yii2-aceeditor": "^2.1.0",
"justinrainbow/json-schema": "^5.2.0",
"opis/json-schema": "^2.0",
"bower-asset/jquery-cookie": "~1.4.1"
},
"autoload": {
Expand Down
26 changes: 12 additions & 14 deletions src/models/crud/WidgetContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

use hrzg\widget\models\crud\base\Widget as BaseWidget;
use hrzg\widget\widgets\Cell;
use JsonSchema\Validator;
use yii\behaviors\TimestampBehavior;
use yii\caching\TagDependency;
use yii\db\Expression;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;
use yii\helpers\Url;
use Opis\JsonSchema\Validator;
use Opis\JsonSchema\Errors\ErrorFormatter;

/**
* Class WidgetContent
Expand Down Expand Up @@ -187,25 +188,22 @@ public function rules()
$rules['validate_properties_json'] = [
'default_properties_json',
function ($attribute) {
/** @var WidgetTemplate $tmpl */
$tmpl = $this->getTemplate()->one();
if (!$tmpl) {
return;
}
$schema = $tmpl->json_schema;
$validator = new Validator();
$obj = Json::decode($schema, false);
$data = Json::decode($this->{$attribute}, false);
$validator->check($data, $obj);
$schema = Json::decode($tmpl->json_schema, false);
$data = Json::decode($this->{$attribute}, false);
$validator->setMaxErrors(9999);
$result = $validator->validate($data, $schema);

if (!$result->isValid()) {
$formatter = new ErrorFormatter();
$errors = $formatter->format($result->error(), false);

if ($validator->getErrors()) {
foreach ($validator->getErrors() as $error) {
$this->addError($error['property'], "{$error['property']}: {$error['message']}");
foreach ($errors as $path => $message) {
$this->addError($path, $path . ': ' . $message);
}
}

}

];
}

Expand Down
31 changes: 13 additions & 18 deletions src/models/crud/WidgetContentTranslation.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@

namespace hrzg\widget\models\crud;

use hrzg\widget\models\crud\base\Widget;
use hrzg\widget\models\crud\base\WidgetTranslation;
use JsonSchema\Validator;
use yii\caching\TagDependency;
use yii\helpers\Json;

use Opis\JsonSchema\Validator;
use Opis\JsonSchema\Errors\ErrorFormatter;

/**
* Class WidgetContentTranslation
Expand All @@ -35,30 +34,26 @@ public function rules()
$rules['validate_properties_json'] = [
'default_properties_json',
function ($attribute) {

/** @var Widget $baseWidget */
$baseWidget = $this->getWidgetContent()->one();
/** @var WidgetTemplate $tmpl */
$tmpl = $baseWidget->getTemplate()->one();
if (!$tmpl) {
return;
}
$schema = $tmpl->json_schema;
$validator = new Validator();
$obj = Json::decode($schema, false);
$data = Json::decode($this->{$attribute}, false);
$validator->check($data, $obj);
$schema = Json::decode($tmpl->json_schema, false);
$data = Json::decode($this->{$attribute}, false);
$validator->setMaxErrors(9999);
$result = $validator->validate($data, $schema);

if (!$result->isValid()) {
$formatter = new ErrorFormatter();
$errors = $formatter->format($result->error(), false);

if ($validator->getErrors()) {
foreach ($validator->getErrors() as $error) {
$this->addError($error['property'], "{$error['property']}: {$error['message']}");
foreach ($errors as $path => $message) {
$this->addError($path, $path . ': ' . $message);
}
}

}

];
}

return $rules;
}

Expand Down