eluhr/yii2-json-attribute-behavior
最新稳定版本:1.0.0
Composer 安装命令:
composer require eluhr/yii2-json-attribute-behavior
包简介
This behavior automatically decodes attributes from JSON to arrays before validation, handling errors and re-encoding if validation fails.
README 文档
README
This behavior automatically decodes attributes from JSON to arrays before validation, handling errors and re-encoding if validation fails. With this a "real" json string can be further processed.
Installation
The preferred way to install this extension is through composer.
Either run
composer require --prefer-dist eluhr/yii2-json-attribute-behavior "*"
or add
"eluhr/yii2-json-attribute-behavior": "*"
to the require section of your composer.json file.
Usage
In a yii\base\Model or a derivation thereof, the behavior can be used as follows:
public function behaviors(): array { $behaviors = parent::behaviors(); $behaviors['json-attribute'] = [ 'class' => eluhr\jsonAttributeBehavior\JsonAttributeBehavior::class, 'attributes' => [ 'data_json' ] ]; return $behaviors; }
By using this behavior it does not matter if the attribute is a string or an array. The behavior will always ensure, that the attribute is an array before saving the data to the database and yii will handle the rest.
This behavior supports i18n. By adding the json-attribute-behavior category in your config you can overwrite the default error messages.
Testing
After installing dependencies via composer you can run the tests with:
make test
统计信息
- 总下载量: 1.62k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-12-22