Closed finnan444 closed 4 years ago
I think the value formatting can solve your problem:
echo $form->field($model, 'data', [
'inputOptions' => [
'value' => is_array($model->data) ? Json::encode($model->data) : $model->data
]
])->textarea();
@berosoboy Thanks for decision, it has 1 problem:
After i save my form it saves this filed like that "{\"icon\":\"fa fa-circle-o\"}"
and Model cant longer convert it to array.
Plus it is unnecessary convertation (model decodes Json, then we encode it again).
May be add this logic to field or create something like jsonArea()/JsonInput?
@finnan444 Understood. In that case you need to decode the json field before saving to database (you can do this after $model->load()
)
...
if ($model->load(Yii::$app->request->post()) {
$model->data = Json::decode($model->data);
...
}
Maybe these steps can be automated by framework.
@finnan444 Probably you have Json::encode() somewhere in you code. Since 2.0.14 the framework encodes json sql fields, so it isn't necessary to convert it. If you want to preserve old behavior (before 2.0.14) check https://github.com/yiisoft/yii2/issues/15716#issuecomment-368143206
Guys, i found where Json is encoded - look at the Class yii\db\pgsql\JsonExpressionBuilder it has build method, see line 45:
$placeholder = $this->queryBuilder->bindParam(Json::encode($value), $params);
so as our form sends correct Json string via post, in JsonExpressionBuilder it is encoded again
@finnan444 : See my prevous comment. Remove Json::encode from your code or disable using Class yii\db\pgsql\JsonExpressionBuilder . How to do it is described here: https://github.com/yiisoft/yii2/issues/15716#issuecomment-368143206
@finnan444 , decoding the data before saving solved your problem?
@schmasterz if i disable this class in config like in #15716, it affects the whole project - so in other components JSON is decoded correctly while been populated.
Dont try to solve my specific issue. Look wider, is there a need to check strings in yii\db\pgsql\JsonExpressionBuilder
and if they are already valid Json and there is no need to encode them again
This behavior is documented: https://github.com/yiisoft/yii2/blob/master/docs/guide/db-active-record.md#json-in-mysql-and-postgresql I think we need to be aware of it before saving the ActiveRecord.
There is an issue https://github.com/yiisoft/yii2/issues/15837 where author asks whether it is possible to decode JSON {}
to object. The discussion reaches the point where we need a possibility to specify encoding/decoding options per-attribute of a specific model.
The problem you report is partially related to that issue. I would suggest you to use AttributeTypecastBehavior with typecastAfterFind = true
on your model to cast array back to json
What steps will reproduce the problem?
What is the expected result?
Form should be loaded and json field edited as string. !Or make new method for json editing in Active field (see https://www.yiiframework.com/doc/api/2.0/yii-widgets-activefield)
What do you get instead?
Additional info