A
A
agent11562016-08-31 19:37:26
Yii
agent1156, 2016-08-31 19:37:26

Working with the form, automatic substitution, how to do it?

I have registered on the site, how can I insert an automatic value in comment_author so that the user does not write the name ?
<?php $form->field($model, 'comment_author')?>

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Michael, 2016-08-31
@springimport

$model->comment_author = 111;

<?php $form->field($model, 'comment_author')?>

A
Andrew, 2016-08-31
@mhthnz

In the controller action:

public function actionComment()
{
    ..............
    ...............
    if (!Yii::$app->user->isGuest) {
        $model->comment_author = Yii::$app->user->identity->username;
    }
    return $this->render('comment', ['model' => $model]);
}

In general, isn't it easier to hide the field with the username if it is authorized? Just substitute it before $model->save().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question