Creating default object from empty value on form submit


(SomebodyHere) #1

I’m trying to create a form to edit some data, and I am getting the following error:

Creating default object from empty value

when I try to submit the form. The error is at $resource->title = $this->title (see model code below). I don’t know what’s going on, because I’ve never had this problem before. I looked up Creating default object from empty value and Creating default object from empty value and Yii2 - Creating default object from empty value but neither of these helped.

Relevant code is attached below.


Inside controller:

<?php

namespace app\modules\studyshare\controllers;

use Yii;
use yii\web\Controller;
use common\models\Resource;
use common\models\ElementLink;
use common\models\ElementLinkForm;
use common\models\CreateNewElementTextForm;

class CreateController extends Controller
{

    public function actionNewElementLink($action = 'create', $id = null)
    {
        $model = new ElementLinkForm();
        ...

        if (($action == 'edit') && ($id != null))
        {
            $resource = Resource::findOne($id);
            $elementLink = ElementLink::findOne($id);
            
            if (($model->load(Yii::$app->request->post())) && ($model->editElementLink())) 
            {
                return $this->redirect(['main/index']);
            } 
            else 
            {
                $model->title = $resource->title;
                $model->description = $resource->description;
                $model->copyright = $resource->copyright;
                $model->editing = $resource->editing;
                $model->path = $elementLink->path;
                $model->id = $id;
            }
        }

        ...
        return $this->render('newElementLink', [
            'model' => $model,
            'id' => $id,
            'action' => $action,
        ]);

In view:

<?php

use macgyer\yii2materializecss\widgets\form\ActiveForm;
use yii\helpers\Html;

$this->title = "Create New Element Link";

?>

<div class = "row">
  <div class = "col m6 push-m3 card">
    
    <div class = "card-content">
      
      <h1>Element Link (<?= $action ?>)</h1>
    
      <?php $form = ActiveForm::begin(); ?>

        <?= $form->field($model, 'path')->textInput(['autofocus' => true]) ?>
        <?= $form->field($model, 'title') ?>
        <?= $form->field($model, 'description')->textarea() ?>
        <?= $form->field($model, 'copyright') ?>
        <?= $form->field($model, 'editing')
              ->dropdownList([
                  1 => 'Myself Only',
                  3 => 'Anyone on MHC',
              ]) ?>
        <?= Html::activeHiddenInput($model, 'id', ['value' => $id]) ?>
        
        <small>Note that regardless of which editing option you choose, each element is editable by MHC Admins and Team members.</small>
        <br /><br />
        <div class="form-group">
          <?= Html::submitButton('<i class = "material-icons right">arrow_forward</i>Go', ['class' => 'btn text-white', 'name' => 'element-link-button']) ?>
        </div>
      
      <?php ActiveForm::end(); ?>
      
    </div>
    
  </div>
</div>

In model:

<?php

namespace common\models;

use Yii;
use yii\base\Model;
use common\models\ElementLink;
use common\models\Resource;

/**
 * is the model behind the contact form.
 */
class ElementLinkForm extends Model
{
    public $title;
    public $description;
    public $copyright;
    public $editing;
    public $path;
    public $id;
    
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            
            [['title', 'copyright', 'editing', 'path'], 'required'],
            
            [['title', 'description', 'copyright', 'editing', 'path', 'id'], 'trim'],
            
            ['title', 'string', 'min' => 2, 'max' => 255],
            ['description', 'string', 'max' => 500],
            ['copyright', 'string', 'min' => 2, 'max' => 255],
            ['editing', 'integer'],
            ['path', 'string', 'min' => 5, 'max' => 2000],
            ['id', 'integer']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'path' => "Web Link"
        ];
    }

    public function createNewElementLink()
    {
        $time = date('Y-m-d H:m:s', time());
        
        $resource = new Resource();
        $resource->type = 2;
        $resource->added_by_type = 1;
        $resource->added_by = Yii::$app->user->identity->id;
        $resource->title = $this->title;
        $resource->description = $this->description;
        $resource->copyright = $this->copyright;
        $resource->editing = $this->editing;
        $resource->approval = 1;
        $resource->created_at = $time;
        $resource->updated_at = $time;
        $resource->save();
        
        $id = Resource::find()
            ->where([
                'added_by' => Yii::$app->user->identity->id,
                'created_at' => $time,
            ])
            ->one();
        
        $link = new ElementLink();
        $link->id = $id['id'];
        $link->path = $this->path;
        
        return $link->save();
    }
    
    public function editElementLink()
    {
        $time = date('Y-m-d H:m:s', time());
        
        $resource = Resource::find($this->id);
        $resource->title = $this->title;
        $resource->description = $this->description;
        $resource->copyright = $this->copyright;
        $resource->editing = $this->editing;
        $resource->updated_at = $time;
        
        $link = ElementLink::findOne($this->id);
        $link->path = $this->path;
        
        return $link->save() && $resource->save();
    }
}

Thanks!


(BANOTHU RAMESH NAIK) #2

The problem is that
$resource = Resource::find($this->id); need to find and get the resource object value in $resource then only you can use in below lines $resource variable as object
like $resource->title = $this->title in model.
But $resource is getting null in your case that leading to above-stated error(Creating default object from empty value).

Can you echo/print and check what is the value your getting for $this->id in the model.
I believe $this->id could be coming null or some other value than the expected one, which resulting resource object as null.

Please check $this->id value and revert back

Thanks


(SomebodyHere) #3

Whoops, I see where my problem was. I wrote Resource::find($this->id rather than Resource::findOne($this->id). Thanks!