1. Utilizar el widget TextArea con la propiedad autoSize:
use yii\widgets\ActiveForm;
use yii\widgets\TextArea;
$form = ActiveForm::begin();
echo $form->field($model, 'my_field')->widget(TextArea::class, [
'options' => [
'rows' => 3,
'style' => 'resize:none;', // Deshabilita el redimensionamiento manual
],
'pluginOptions' => [
'autoSize' => [
'enable' => true,
'maxLines' => 5, // Número máximo de líneas
'minLines' => 3, // Número mínimo de líneas
],
],
]);
ActiveForm::end();
2. Utilizar el widget TinyMCE con la propiedad autogrow:
use dosamigos\tinymce\TinyMce;
echo $form->field($model, 'my_field')->widget(TinyMce::class, [
'options' => [
'rows' => 3,
],
'pluginOptions' => [
'autogrow_onload' => true,
'autogrow_minimum_height' => 100,
'autogrow_maximum_height' => 400,
'autogrow_bottom_margin' => 20,
],
]);
3. Utilizar JavaScript para ajustar automáticamente el tamaño del campo:
use yii\helpers\Html;
echo Html::activeTextArea($model, 'my_field', [
'rows' => 3,
'style' => 'resize:none;', // Deshabilita el redimensionamiento manual
'onInput' => 'this.style.height = this.scrollHeight + "px";',
]);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter