Jan Beeck Jan Beeck - 1 year ago 215
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]) ?>

Answer Source

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() ?>
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download