0

I have Settings controller like

public function actionIndex()
{
    $model = new SettingsForm;

    if(isset($_POST['SettingsForm'])) {
        if($model->validate()) {
            //
        }
    }

    $this->render('index', array('model' => $model));
}

and in settings view:

<?php
$form = $this->beginWidget(
    'CActiveForm', array(
       'id' => 'settings-form',
       'enableClientValidation' => true,
       'clientOptions' => array(
           'validateOnSubmit' => true,
       ),
));
?>
<div class="form-group">
<?php echo $form->labelEx($model, 'meta_keywords'); ?>
<?php echo $form->textField($model, 'meta_keywords', array('class' => 'form-control', 'value' => Yii::app()->config->get('meta_keywords'), 'placeholder' => 'Ключевые слова и фразы через запятую')); ?>
<?php echo $form->error($model, 'meta_keywords', array('class' => 'text-danger')); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model, 'main_page'); ?>
<?php echo $form->dropDownList($model, 'main_page', $model->getPages()); ?>
<?php echo $form->error($model, 'main_page', array('class' => 'text-danger')); ?>
</div>

function getPages in SettingsForm model:

public function getPages() {
        return array(
            0 => 'Nothing'
        );
    }

This code returns error:

Property "SettingsForm.main_page" is not defined.

But all previos elements Yii created successfully and don't return any error =\

Cœur
  • 37,241
  • 25
  • 195
  • 267
heeeej
  • 9
  • 1

4 Answers4

0

In your SettingsForm model is main_page defined in the rules method? i.e.

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
        array( ... 'main_page', ...),
        ...
    );
}
Stu
  • 4,160
  • 24
  • 43
  • hmm... and `var_dump($model->main_page)` returns anything, or does that return an error too? You could also try `array('main_page', 'safe')` in the rules array too, just to see if it's an issue with the booleanValidator? – Stu Nov 26 '14 at 13:32
  • @heeeej, still please post in question (as update) the whole model validation rules. – Igor Savinkin Nov 26 '14 at 14:28
0

Updated

class SettingsForm extends CFormModel

Still do it. Look at here and here (rus).

public function rules()
{        
    return array(
        array('site_name, charset, meta_description, meta_keywords', 'required'),
        array('main_page', 'boolean'),
        array('site_name, charset, meta_description, meta_keywords, main_page', 'safe'),
    );
}
Igor Savinkin
  • 5,669
  • 8
  • 37
  • 69
  • i did echo $form->dropDownList($model, 'main_page', array(0 => 'nothing')); and.... NOTHING +) – heeeej Nov 26 '14 at 14:07
  • @heeeej, how about in dropdown you just put `$form->dropDownList($model, 'main_page', array(0=>'smth'))` will it work? – Igor Savinkin Nov 26 '14 at 14:21
  • @heeeej, also, do you need ClientValidation to be ON? Try to turn it off: `'enableClientValidation' => false,` since you validate in server and in dropdown nothing is inputed from outside. – Igor Savinkin Nov 26 '14 at 14:26
  • @heeeej, still would you post in your question (as update) the whole model validation rules like these: `public function rules() { return array( array('userId, email', 'required'),... ` – Igor Savinkin Nov 26 '14 at 14:42
  • I posted.. I called all fields =( – heeeej Nov 26 '14 at 14:45
0
/**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('site_name, charset, meta_description, meta_keywords', 'required'),
            array('main_page', 'boolean')
        );
    }
heeeej
  • 9
  • 1
  • sure, you did not define `array('site_name, charset, meta_description, meta_keywords, main_page', 'safe', 'on'=>'search'),` as the last line in this array. So they are not SAFE for storing in AR and DB. Do it. – Igor Savinkin Nov 26 '14 at 14:50
0

Pfffff.... I just forgot call variable in model $main_page...

class SettingsForm extends CFormModel
{
    public $site_name;
    public $charset;
    public $meta_description;
    public $meta_keywords;
    public $main_page;

..
}
heeeej
  • 9
  • 1