Jan Beeck Jan Beeck - 2 months ago 17
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.

<?php

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

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

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

Answer

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

if($model->isNewRecord){
$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() ?>