1

I create a CRUD called Channel and a CRUD Post, so I want to add create Post form to DetailView of Channel; example when a user view Channel Alpha under the Alpha details he have a form from Post to create a Post inside that Channel

user can view the detail of a Channel and also can add Post to that Channel

something similar to :

in Channel controller

public function actionView($id)
    {
        $ly_addPost = new Posts();
        return $this->render('view', [
            'model' => $this->findModel($id),
            'addpost' => $ly_addPost,
        ]);
    }

and in channel view I did edit it to :

//Yii2 code

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\models\Channel */

$this->title = $model->Channel_name;
$this->params['breadcrumbs'][] = ['label' => 'Channels', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="channel-view">

    <h1><?= Html::encode($this->title) ?></h1>

    <p>
        <?= Html::a('Update', ['update', 'id' => $model->Channel_id], ['class' => 'btn btn-primary']) ?>
        <?= Html::a('Delete', ['delete', 'id' => $model->Channel_id], [
            'class' => 'btn btn-danger',
            'data' => [
                'confirm' => 'Are you sure you want to delete this item?',
                'method' => 'post',
            ],
        ]) ?>
    </p>
    <div class="col-md-12">
        <?= $this->render ('_form', [
            'addpost' => $ly_addPost,
        ])
        ?>

        <div class="posts-form">

            <?php $form = ActiveForm::begin(); ?>

            <?= $form->field($model, 'Posts_title')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'Posts_text')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'Posts_file')->textInput(['maxlength' => true]) ?>

            <?php //= $form->field($model, 'Posts_crdate')->textInput() ?>

            <?= $form->field($model, 'Channel_id')->textInput(['maxlength' => true]) ?>

            <?= $form->field($model, 'Permissions_id')->textInput() ?>

            <?php //= $form->field($model, 'user_id')->textInput() ?>

            <div class="form-group">
                <?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']) ?>
            </div>

            <?php ActiveForm::end(); ?>

        </div>
    </div>

</div>

but I get error :

PHP Notice – yii\base\ErrorException

Undefined variable: ly_addPost

2 Answers 2

2

Change addpost to ly_addPost show below

public function actionView($id)
{
    $ly_addPost = new Posts();
    return $this->render('view', [
        'model' => $this->findModel($id),
        'ly_addPost' => $ly_addPost,
    ]);
}
Sign up to request clarification or add additional context in comments.

Comments

1

Just Change $ly_addPost to $addpost in view file

<div class="col-md-12">
        <?= $this->render ('_form', [
            'addpost' => $addpost,
        ])
        ?>
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.