yii2 beginWidget -> unknown method

6.4k views Asked by At

In yii I had such code which was roking OK

$form=$this->beginWidget('CActiveForm', array(
    'id'=>'ride-form',
    'enableClientValidation'=>false,
    'clientOptions'=>array(
        'validateOnSubmit'=>true,
    ),
));

now I am trying to use it in Yii 2 version. But I'm getting error

Calling unknown method: yii\web\View::beginWidget()

Why ? and what class maybe I should additionally use to fix this problem ?

2

There are 2 answers

0
arogachev On BEST ANSWER

This error appears because yii\web\View class simply does not have the method beginWidget().

For working with forms in Yii2 use ActiveForm widget.

Replace your code with:

use yii\widgets\ActiveForm;    

$form = ActiveForm::begin([
    'id' => 'ride-form',
    'enableClientValidation'=>false,
    'validateOnSubmit' => true, // this is redundant because it's true by default
]);

// ...

ActiveForm::end();
0
Roger On

2 things i would check for if a method is unknown.

1) is the class included before you call it?

2) Are you creating an instance of the class and calling the class from that instance variable?

include 'myClass.php';

$class = new myClassName();

$class->method();