| Conditions | 5 |
| Paths | 6 |
| Total Lines | 19 |
| Code Lines | 13 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
| 1 | <?php |
||
| 31 | public function validateAttribute($model, $attribute): void |
||
| 32 | { |
||
| 33 | $value = $model->$attribute; |
||
| 34 | $error = null; |
||
| 35 | if (!empty($value) && is_string($value)) { |
||
| 36 | $json = Json::decodeIfJson($value); |
||
| 37 | if (!is_array($json)) { |
||
| 38 | $error = Craft::t( |
||
| 39 | 'codefield', |
||
| 40 | 'This is not valid JSON', |
||
| 41 | ); |
||
| 42 | } |
||
| 43 | } else { |
||
| 44 | $error = Craft::t('codefield', 'Is not a string.'); |
||
| 45 | } |
||
| 46 | // If there's an error, add it to the model, and log it |
||
| 47 | if ($error) { |
||
| 48 | $model->addError($attribute, $error); |
||
| 49 | Craft::error($error, __METHOD__); |
||
| 50 | } |
||
| 53 |