S
S
Screamy Di2016-09-20 15:50:56
Yii
Screamy Di, 2016-09-20 15:50:56

Yii2 ActiveForm not accepting values ​​from model?

Hello!
Straight to the point. The task is trivial to impossibility. We write a feedback form (in the form of a questionnaire), wrote them more than once and everything was ok. The model itself is NOT connected to the database, and immediately after validation sends a letter to the mail.

namespace frontend\models;

use yii\base\Model;

class QuestionaryForm extends Model
{
   //.. для краткости пишу не все поля
    public $organization;
    public $position;
    public $username;

public function scenarios()
    {
        //...тут все как обычно 
    }

    //... тут правила валидации
    public function rules(){}
}

Then the actual form, also nothing new
<article>
            <? $form = \yii\widgets\ActiveForm::begin() ?>

            <?= $form->field($model, 'organization')?>
            <?= $form->field($model, 'position')?>
            <?= $form->field($model, 'username')?>

            <?= \yii\helpers\Html::submitButton("Отправить", ['class' => 'btn btn-w-svg']) ?>

            <? \yii\widgets\ActiveForm::end();?>
</article>

In the controller, too, everything is standard
public function actionFeedback()
    {
        $model = new QuestionaryForm();
        $model->scenario = 'default';

       //... здесь действия после отправки формы        

        return $this->render('questionary', ['model' => $model]);
    }

The most interesting begins at the exit. When you open a page with a form, literally everything falls.
More specifically. Templates are not loaded, none at all. Styles and scripts are also by. And the YII debugger too. In the google inspector in the head tag nothing at all.
There are no errors and all the output before the start of ActiveForm is present on the page.
var_dump($model); - returns quite a live QuestionaryForm model with fields == NULL
If you leave only this:
<article>
            <? $form = \yii\widgets\ActiveForm::begin() ?>

            <?= \yii\helpers\Html::submitButton("Отправить", ['class' => 'btn btn-w-svg']) ?>

            <? \yii\widgets\ActiveForm::end();?>
</article>

That all at once works. Tried a lot of things, but the head no longer understands. There is a feeling that the problem is right under my nose, but I do not see it. I would be grateful if you poke your finger))) To
future generations. I decided myself.
The error was in rules(). Specified instead of the "string" validator, the "text" validator (I often used it in the previous project and, apparently, got used to it). Changed it and everything started up.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-09-20
@slo_nik

Good afternoon.
About loading styles, scripts. You have in the template, in the head
Take the feedback form, which is set by default when you install yii2 itself, check it out.

A
Anton, 2016-09-20
@karminski

Well, it's empty here:
Declare the fields that come in the form as safe - and you'll be happy.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question