Closed rizrob66 closed 7 years ago
:-)
Hi. Could you paste a code sample?
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 type of element is object and not string
You haven't given any code that's used by the settings component.
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;
?>
<?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(); ?>`
Since you're using your own setting model you should configure that in the settings component and implement SettingInterface
.
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.
:-)
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
Having same issue , Tried adding filter => 'floatval' , but it didn't solved the problem. @arisk , can you please explain your answer ?
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