Jan Beeck Jan Beeck - 23 days ago 5x
PHP Question

How to set a value in textarea Yii2 activeform from a variable?

I have the following code, how to set a value from a variable in the textarea? Thanks.


$connection = \Yii::$app->db;

$sql = 'SELECT user_profile_name FROM users_profiles WHERE user_id = :_id';
$command = $connection->createCommand($sql);
$username = $command->queryScalar();

<?= $form->field($model, 'name')
->textInput(['editable' => true,'value' => $username]) ?>


Is you need set value when create new record. Please set with:

$model->name= 'Value';


Because, when update, will show name. So not need set from form field.

and show as textarea field:

<?php echo $form->field($model, 'name')->textarea() ?>