phemellc / yii2-settings

Yii2 Settings Module
Other
151 stars 74 forks source link

Cannot use SettingAction form a custom form #49

Open rizrob66 opened 8 years ago

rizrob66 commented 8 years ago

Hi, I tried to use SettingAction to make a custom form to handle a setting category. There are several things I did not understand, so it doesn't work. Could you please provide an fully working example? thanks Roberto

arisk commented 8 years ago

Hi Roberto. There's an example of how to use the settings action in the readme file. If you have any problems with those instructions please show me what you have so far and I'll try to help you get it working.

rizrob66 commented 8 years ago

Thanks arisk, I work with basic and made the model in models\AziendaSettings.php directory:

<?php
namespace app\models;
use Yii;
class AziendaSettings extends \yii\base\Model {
    public $titolare, $telefono;
    public function rules()
    {
        return [
            [['titolare', 'telefono'],
        ];
    }

    public function fields()
    {
            return ['titolare', 'telefono'];
    }

    public function attributes()
    {
            return ['titolare', 'telefono'];
    }
}

in views\site\azienda-settings.php

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
?>

<?php $form = ActiveForm::begin(['id' => 'azienda-settings-form']); ?>

<?= $form->field($model, 'titolare') ?>
<?= $form->field($model, 'telefono') ?>

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

in controllers\SiteController.php:

  public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],

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

when I try index.php?r=site/azienda-settings There is no submit button and I can't see the actual value of the varable azienda.titolare and azienda.telefono . You Settings is very usefull, please help me to complete.

demogorgorn commented 8 years ago

I think that you forgot to add submit button in your view:

<?php $form = ActiveForm::begin(['id' => 'social-service-form']); ?>
<?= $form->field($model, 'twitter') ?>
<?= $form->field($model, 'google') ?>
<?= $form->field($model, 'facebook') ?>
<?= $form->field($model, 'youtube') ?>
<?= $form->field($model, 'linkedin') ?>
<?= $form->field($model, 'pinterest') ?>
<?= $form->field($model, 'vimeo') ?>
<?= $form->field($model, 'instagram') ?>
<?= $form->field($model, 'vk') ?>
<?= $form->field($model, 'ok') ?>

<div class="form-group">
        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>
    </div>

    <?php ActiveForm::end(); ?>
rizrob66 commented 8 years ago

thanks demogorgorn, I add the submit button as you suggester and: Invalid Configuration – yii\base\InvalidConfigException Unable to locate message source for category 'extensions/yii2-settings/settings'. ..... Module::t('settings', '{attribute} "{value}" already exists for this section.') :-(

arisk commented 8 years ago

Have a look at this comment to configure your translations correctly. https://github.com/phemellc/yii2-settings/issues/51#issuecomment-221190859

rizrob66 commented 8 years ago

Yesss!!!!!!! it works!!!!! Thank you very much indeed! :-)

arisk commented 8 years ago

You're welcome :)

celsoft commented 6 years ago

I can not make a custom form.

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

clip2net_171226113340

Controller: clip2net_171226113742

Model: clip2net_171226113755

print_r($model) in view: clip2net_171226113930

UPDATE: If you just save numbers in the text box, then this error occurs. If the text field, then all is well.

mohdqasim98 commented 6 years ago

If you just save numbers in the text box, then this error occurs. If the text field, then all is well.

having the same issue. Any fix ?