The moment a user needs to write more than a short phrase — a comment, a description, a message body — a single-line textfield stops being the right tool. textarea is Drupal's multi-line equivalent, and it comes with its own small set of properties worth knowing well.
What you'll learn in this lesson
- The basic textarea definition and its sizing properties
- Why textarea has no
#maxlengthby default, and how to add one yourself - When to reach for a full text-format-aware field instead of a plain textarea
A basic textarea
$form['message'] = [
'#type' => 'textarea',
'#title' => $this->t('Message'),
'#rows' => 5,
'#description' => $this->t('Tell us what you need help with.'),
];
#rows sets the visible height in text lines — purely cosmetic, exactly like #size on a textfield, and doesn't limit how much a user can actually type. There's no #cols equivalent in modern Drupal forms; width is handled by CSS, not a render property.
Enforcing a length limit yourself
$form['message']['#maxlength'] = 1000;
Unlike textfield, a plain textarea doesn't automatically get client-side character-count enforcement from #maxlength in every Drupal version — check your target version's behavior, and back it up with a real server-side check in validateForm() if the limit genuinely matters (for example, matching a database column's storage limit). Never rely on client-side enforcement alone for anything you actually need to be true.
When textarea isn't enough: text-format-aware fields
A plain textarea stores exactly what the user typed — no HTML, no formatting, no filtering. The moment you need rich text with a format selector and Drupal's text-filtering pipeline (the same system that powers a node body field), reach for #type => 'text_format' instead, which pairs a textarea with a format dropdown and runs the result through whichever text format the user selected.
textarea value is never run through Drupal's text filter system — if you print it back out later, you're responsible for your own sanitization (typically via Html::escape() for plain text, never by trusting it as safe markup). Only text_format fields carry filtering built in.Quick check: you need a comment field that should support basic bold/italic formatting, filtered safely. Plain
textareaortext_format? (text_format— it pairs the textarea with a format selector and runs the saved value through Drupal's text-filtering pipeline, which is exactly the safety net formatted content needs.)
Key takeaways
#rowscontrols visible height only — it doesn't limit input length.- Don't trust client-side
#maxlengthalone on a textarea — back genuinely important limits with server-side validation. - Use plain
textareafor unformatted text you'll escape yourself; usetext_formatwhen you need Drupal's filtering pipeline for rich content.
Coming up next
Text input covers free-form entry. Next up is select — the element for when you want to constrain the user to a fixed, known set of choices.