M
M
Mors Clamor2019-09-18 22:49:49
Yii
Mors Clamor, 2019-09-18 22:49:49

Yii2 form processing without ActiveForm?

All good. I started to pick Yii, not without the help of kind people, an unpleasant feature was discovered - bootstrap 4 is not supported. I installed the component, registered the configs, everything works fine. Until I insert the widget. As soon as I insert a widget - an error

Method yii\widgets\ActiveField::__toString() must not throw an exception, caught Error: Call to a member function isAttributeRequired() on null

To be honest, I'm not sure if bootstrap is the issue.
A piece of a template with a form:
<?use yii\widgets\ActiveForm;?>
....
<?
          $form = ActiveForm::begin([
    'id' => 'login-form',
    'options' => ['class' => 'landing-form auth align-middle'],
]) ?>
    <?= $form->field($model, 'username') ?>
    <?= $form->field($model, 'password')->passwordInput() ?>

    <div class="form-group">
        <div class="col-lg-offset-1 col-lg-11">
            <?= Html::submitButton('Вход', ['class' => 'btn btn-primary']) ?>
        </div>
    </div>
<?php ActiveForm::end() ?>

The controller that calls this view (action):
//В use всё подключено
  public function actionLogin() {
    return $this->render("landing");

    $model = new LoginForm();
    if ($model->load(Yii::$app->request->post() && $model->validate())) {
        return $this->goBack();
    }
    return $this->render('landing', [
        'model' => $model,
    ]);
  }

And the LoginForm model itself:
<?php

namespace app\models;

use Yii;
use yii\base\Model;

/**
 * LoginForm is the model behind the login form.
 *
 * @property User|null $user This property is read-only.
 *
 */
class LoginForm extends Model
{
    public $username;
    public $password;

    /**
     * @return array the validation rules.
     */
    public function rules()
    {
        return [
            // username and password are both required
            [['username', 'password'], 'required'],
        ];
    }


    public function validate()
    {
      return true;
    }
}
?>

That is, I did it elementary for verification, but I don’t understand what is the cause of the error

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2019-09-19
@66demon666

Your mistake is here

public function actionLogin() {
    return $this->render("landing");

   // ......

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

You are connecting a view with a form, where you do not pass the form itself. Then you process the data from the form and again connect the same view. In the view, the widget is waiting for form attributes, but there are none, hence the error
. So bootstrap has absolutely nothing to do with it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question