phemellc / yii2-settings

Yii2 Settings Module
Other
151 stars 74 forks source link

Custom form, numeric input problem #68

Closed rizrob66 closed 7 years ago

rizrob66 commented 7 years ago

Hi, in my custom form I added a normal string field. When I save the data if I insert a string everything is ok but if I enter a number an error occurs. PHP Recoverable Error – yii\base\ErrorException Object of class stdClass could not be converted to string I noticed that it does not store the data as string type but as object type? Could you help me, please? Roberto

rizrob66 commented 7 years ago

:-)

arisk commented 7 years ago

Hi. Could you paste a code sample?

rizrob66 commented 7 years ago

The code is very simple: Form: `<?php $form = ActiveForm::begin(['id' => 'set-calendario-form', 'type' => ActiveForm::TYPE_HORIZONTAL,]); ?>

<?= 'Per semina' ?>

<?= $form->field($model, 'inizio_pre_semina', [ 'addon' => ['prepend' => ['content'=>'']]])->hint('formato gg-mm (Es. 03-02)') ?> <?php echo $form->field($model, 'percentuale_pre_semina', [ 'addon' => ['prepend' => ['content'=>'%']]]) ?>

    <?= Html::submitButton('Salva', ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?> model: <?php namespace app\models; use Yii;

class calendario extends \yii\base\Model { public $inizio_pre_semina, $percentuale_pre_semina;

public function rules()
{
    return [ 
        [['inizio_pre_semina', 'percentuale_pre_semina', 
               ],  'string'],
    ];
}

... ` The field percentuale_pre_semina in the model is string if you insert a string like "pippo" the element was saved correctly if you insert 10 (number) the error

PHP Recoverable Error – yii\base\ErrorException Object of class stdClass could not be converted to string

the type of element is object and not string

arisk commented 7 years ago

You haven't given any code that's used by the settings component.

rizrob66 commented 7 years ago

I did not understand what you mean, however, I'll write down all the code below. If I forgot something let me know

SiteController.php .... 'calendario-settings' => [ 'class' => 'pheme\settings\SettingsAction', 'modelClass' => 'app\models\calendario', //'scenario' => 'site', // Change if you want to re-use the model for multiple setting form. 'viewName' => 'calendario-settings' // The form we need to render ],

app\model\calendario.php `<?php

namespace app\models;

use Yii;

class calendario extends \yii\base\Model { public $inizio_pre_semina, $percentuale_pre_semina;

public function rules()
{
    return [ 
        [['inizio_pre_semina', 'percentuale_pre_semina', 
          ],  'string'],
    ];
}

public function fields()
{
        return ['inizio_pre_semina','percentuale_pre_semina', 
          ];
}

public function attributes()
{
        return ['inizio_pre_semina','percentuale_pre_semina', 
          ];
}

public function attributeLabels() { return [ 'inizio_pre_semina' => 'Inizio pre-semina', 'percentuale_pre_semina' => '% pre-semina', ]; }
public function attributeHints() { return [ 'inizio_pre_semina'=>'formato gg-mm (Es. 15-03) Inserire solo il segno meno (-) per il primo giorno utile.', ]; }

} view\site\calendario-settings.php <?php

use yii\helpers\Html;

use kartik\widgets\ActiveForm; //use yii\helpers\ArrayHelper; //use kartik\select2\Select2;

//use app\models\Scelte; //use app\models\Banche; use kartik\icons\Icon;

$icon = Icon::show('settingsthree-gears', [], Icon::WHHG); $this->title = 'Settaggio paramenti calendario'; $this->params['breadcrumbs'][] = $this->title;

?>

title) ?>

<?php $form = ActiveForm::begin(['id' => 'set-calendario-form', 'type' => ActiveForm::TYPE_HORIZONTAL,]); ?>

<?= 'Per semina' ?>

<?= $form->field($model, 'inizio_pre_semina', [ 'addon' => ['prepend' => ['content'=>'']]])->hint('formato gg-mm (Es. 03-02)') ?> <?php echo $form->field($model, 'percentuale_pre_semina', [ 'addon' => ['prepend' => ['content'=>'%']]]) ?>

    <?= Html::submitButton('Salva', ['class' => 'btn btn-success']) ?>

<?php ActiveForm::end(); ?>`

arisk commented 7 years ago

Since you're using your own setting model you should configure that in the settings component and implement SettingInterface.

rizrob66 commented 7 years ago

Sorry but I do not understand what you mean. I followed the example provided in the documentation. Please could you give me an example of: How should i configure it in the component? How I should implement SettingInterface?

This is a new thing that did not have to be done in the past, however, in the documentation is not described.

rizrob66 commented 7 years ago

:-)

arisk commented 7 years ago

http://www.yiiframework.com/doc-2.0/guide-concept-configurations.html#configuration-files

https://secure.php.net/manual/en/language.oop5.interfaces.php

dox07 commented 6 years ago

Hi there! I had same problem some time ago. I have resolved like that: add filter rule for field in model custom form. For example:

class SettingsSendPaymentForm extends Model { public $isSendAutoPaymentToClient;

public function rules()
{
    return [
        [['isSendAutoPaymentToClient'], 'integer'],
        [['isSendAutoPaymentToClient'], 'filter', 'filter' => 'intval'],
    ];
}

In this way variable convert to integer type and when run setSettings function gettype return appropriate type of attribute. Thanks

mohdqasim98 commented 6 years ago

Having same issue , Tried adding filter => 'floatval' , but it didn't solved the problem. @arisk , can you please explain your answer ?