Alex
Alex

Reputation: 996

Getting Error "Call to a member function formName() on a non-object"

I'm getting error (Call to a member function formName() on a non-object) while trying to get data from tabular input. Here's my code -

View Code:

$arr = array("config", "specs");
foreach($arr as $index=>$value){
    echo '<h3>' . $value . '</h3>';
    for($i=0; $i<3; $i++){
    echo '<div class="text-row">';
        echo $form->field($configmodel, "[$index]config[$i]")->label(false);
        echo '<p class="invisible"></p>';
    echo '</div>';
    }
}

Controller Code:

$configmodel = new ConfigForm();
$configmodels = [new ConfigForm()];
for($i = 1; $i < 3; $i++) {
    $configmodels[] = new ConfigForm();
}

if($configmodel->load(Yii::$app->request->post())) {
    if(Model::loadMultiple($configmodels, Yii::$app->request->post()) &&  Model::validateMultiple($configmodels)){
        return $this->render('result', [
            'configmodels' => $configmodels
        ]);
    }

} else {
    return $this->render('index', [
        'configmodel' => $configmodel
    ]);
}

Model Code:

class ConfigForm extends Model
{
    public $config = [];

    public function rules()
    {
        return [
            ['config', 'required'],
            ['config', 'each', 'rule' => ['string', 'min' => 20]
        ];
    }
}

Upvotes: 1

Views: 3087

Answers (1)

topher
topher

Reputation: 14860

You are passing a model instead of an array into loadMultiple. From the guide:

$models : array : The models to be populated. Note that all models should have the same class.

The same goes for validateMultiple.

Since you are expecting 3 models you can create an array of these:

$configModels = [new ConfigForm(), new ConfigForm(), new ConfigForm()];
...
if (Model::loadMultiple($configModels, Yii::$app->request->post()) &&  Model::validateMultiple($configModels)  ...

Upvotes: 1

Related Questions