Open jjexperto opened 6 years ago
Do you mind creating a pull request?
Hi, I also have this problem. I created pull request for this issue, https://github.com/phemellc/yii2-settings/pull/77
I think it should be possible to save an empty string.
Could you tell me how? When i'm trying save empty string it saving with type object, because Json::decode with empty string return null, and model type changing to 'object'
Could you elaborate a bit on more how this error is triggered?
I just tried to save an empty value with type object and it works fine for me. Or is this done programatically?
I just played around with it and I can save the following values as object:
2
""
" "
"foo"
but not:
foo
Using version: 0.5
I changed version to 0.5 There is my web.php file:
'components' => [
'settings' => [
'class' => 'pheme\settings\components\Settings',
'frontCache' => null,
'cache' => null,
],
...]
'modules' => [
'settings' => [
'class' => 'pheme\settings\Module',
'sourceLanguage' => 'en',
],
...
]
There is model:
<?php
namespace app\models;
use yii\base\Model;
class Settings extends Model
{
public $testValue;
public function rules()
{
return [
'testValue' => ['testValue', 'string'],
'testValueTrim' => ['testValue', 'trim'],
];
}
public function fields()
{
return [
'testValue',
];
}
public function attributes()
{
return [
'testValue',
];
}
public function attributeLabels()
{
return [
'testValue' => 'Test value',
];
}
}
There is view:
<?php
use yii\widgets\ActiveForm;
use yii\helpers\Html;
?>
<?php $form = ActiveForm::begin(['id' => 'site-settings-form']); ?>
<?= $form->field($model, 'testValue')->textInput() ?>
<?= Html::submitButton(Yii::t('admin','Save'), ['class' => 'btn btn-primary']) ?>
<?php ActiveForm::end(); ?>
And finaly there is my controller:
<?php
namespace app\controllers\admin;
use yii\easyii\components\Controller;
class SettingsController extends Controller
{
public function init()
{
$this->layout = '@app/views/layouts/admin';
\Yii::$app->getModule('settings');
}
public function actions(){
return [
'index' => [
'class' => 'pheme\settings\SettingsAction',
'modelClass' => 'app\models\Settings',
'viewName' => '@app/views/admin/settings/site-settings'
],
];
}
}
I open settings page in admin i see form with 1 field - Test value.
When i entered:
But this error only appears when using SettingsAction
? Can you reproduce it also with the default admin backend?
Personally, I don't like the magic happening in the setters. I'd prefer adding an explicit type setting for these cases, but I also don't see an easy way how to do that - maybe in the customized model....
Just for the record ... my only concern is that a change like this does not break BC.
We had similar issues before:
Yes, I saw these tasks, but did not find a solution in them.
I don't use json objects in the settings, probably because of this I do not fully understand how this should work.
The problem is that the string is then saved with string
or object
type.
P.S. I'll try again with the default admin backend in a few hours.
Apparently this is still an issue
2020 - problem is actual
I'm having a problem when save a settings form with empty fields. Empty field saved as object and get an error because the field is an object instead a string.
PHP Recoverable Error – yii\base\ErrorException Object of class stdClass could not be converted to string
The problem is in setSetting method of BaseSetting class, the method do not consider empty string case and set it as object:
if ($type !== null) { $model->type = $type; } else { $t = gettype($value); if ($t == 'string') { $error = false; try { Json::decode($value); } catch (InvalidParamException $e) { $error = true; } if (!$error) { $t = 'object'; } } $model->type = $t; }