this.renderTree(object, []);
renderTree(object, array, parent = '') {
if(!Array.isArray(object)) {
for( let key in object) {
if(typeof object[key] === 'object') {
if(!Array.isArray(object[key])) {
array.push({
id: array.length,
_key: key,
_value: false,
type: 'object',
childIds: [],
});
this.renderTree(object[key], array, array.length - 1);
} else {
array.push({
id: array.length,
_key: key,
_value: false,
type: 'array',
childIds: [],
});
this.renderTree(object[key], array, array.length - 1);
}
} else {
array.push({
id: array.length,
_key: key,
_value: object[key],
type: 'string',
childIds: [],
});
if(parent) {
array[parent].childIds.push(array.length - 1);
}
}
}
} else {
let index, len;
for(index = 0, len = object.length; index < len; ++index) {
let element = object[index];
if(typeof element === 'object') {
array.push({
id: array.length,
_key: false,
_value: false,
type: 'object',
childIds: [],
});
array[parent].childIds.push(array.length - 1);
this.renderTree(element, array, array.length - 1);
}
}
}
}
let str_entity = {};
if(action.data.newKey != action.data.oldKey) {
for(let key in state.structure) {
if(key == action.data.oldKey) {
str_entity[action.data.newKey] = action.data.newValue;
} else {
str_entity[key] = state.structure[key];
}
}
}
app.get('/entity/add', (req, res) => {
db.createEntity(req.body).then(data => res.send(data));
});
/** Create HTTP server. **/
const server = http.createServer(app);
server.listen(port, () =>
console.log(`Server started on port ${port}`)
);
trait AjaxValidationTrait
{
/**
* Performs ajax validation.
*
* @param Model $model
*
* @throws \yii\base\ExitException
*/
protected function performAjaxValidation(Model $model)
{
if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
echo json_encode(ActiveForm::validate($model));
Yii::$app->end();
}
}
}
class DefaultController extends BaseAdminController
{
use AjaxValidationTrait;
public function actionCreate()
{
$model = new Point();
$this->performAjaxValidation($model);
if ($model->validate()) {
....
} else {
return $this->render('create', [
'model' => $model]
);
}
}
}
<?php $form = ActiveForm::begin([
'enableAjaxValidation' => true,
'enableClientValidation' => false,
]); ?>