Skip to content

Latest commit

 

History

History
173 lines (126 loc) · 8.3 KB

TextArea.md

File metadata and controls

173 lines (126 loc) · 8.3 KB

TextArea

The <textarea> HTML element represents a multi-line plain-text editing control, useful when you want to allow users to enter a sizable amount of free-form text, for example, a comment on a review or feedback form.

Basic Usage

Instantiate the TextArea class using TextArea::widget().

$textArea = TextArea::widget();

Generate field id and name

The fieldAttributes method is used to generate the field id and name for the HTML output.

Allowed arguments are:

  • formModel - The name of the model.
  • property - The name of the field.
// generate field id and name
$textArea->fieldAttributes('model', 'field');

Setting Attributes

Use the provided methods to set specific attributes for the a element.

// setting class attribute
$textArea->class('container');

Or, use the attributes method to set multiple attributes at once.

$textArea->attributes(['class' => 'container', 'style' => 'background-color: #eee;']);

Adding Content

If you want to include content within the textarea tag, use the content method.

$textArea->content('MyContent');

Rendering

Generate the HTML output using the render method, for simple instantiation.

$html = $textArea->render();

Or, use the magic __toString method.

$html = (string) $textArea;

Common Use Cases

Below are examples of common use cases:

// adding multiple attributes
$textArea->class('external')->content('MyContent');

// using data attributes
$textArea->dataAttributes(['analytics' => 'trackClick']);

Explore additional methods for setting various attributes such as lang, name, style, title, etc.

Prefix and Suffix

Use prefix and suffix methods to add text before and after the textarea tag, respectively.

// adding a prefix
$html = $textArea->content('MyContent')->prefix('MyPrefix')->render();

// adding a suffix
$html = $textArea->content('MyContent')->suffix('MySuffix')->render();

Template

The template method allows you to customize the HTML output of the a element.

The following template tags are available:

Tag Description
{prefix} The prefix text.
{tag} The a element.
{suffix} The suffix text.
// using a custom template
$textArea->template('<div>{tag}</div>');

Attributes

Refer to the Attribute Tests for comprehensive examples.

The following methods are available for setting attributes:

Method Description
attributes() Set multiple attributes at once.
autocomplete() Set the autocomplete attribute.
autofocus() Set the autofocus attribute.
class() Set the class attribute.
cols() Set the cols attribute.
content() Set the content within the textarea element.
dataAttributes() Set multiple data-attributes at once.
dirName() Set the dirname attribute.
disabled() Set the disabled attribute.
form() Set the form attribute.
id() Set the id attribute.
lang() Set the lang attribute.
name() Set the name attribute.
placeholder() Set the placeholder attribute.
readOnly() Set the readonly attribute.
rows() Set the rows attribute.
style() Set the style attribute.
tabIndex() Set the tabindex attribute.
title() Set the title attribute.
type() Set the type attribute.
wrap() Set the wrap attribute.

Custom methods

Refer to the Custom Methods Tests for comprehensive examples.

The following methods are available for customizing the HTML output:

Method Description
fieldAttributes() Generate the field id and name for the HTML output.
prefix() Add text before the tag element. If empty, the prefix tag will be disabled.
prefixAttributes() Set attributes for the prefix element.
prefixClass() Set the class attribute for the prefix element.
prefixTag() Set the tag for the prefix element.
If false the prefix tag will be disabled.
render() Generates the HTML output.
suffix() Add text after the tag element. If empty, the suffix tag will be disabled.
suffixAttributes() Set attributes for the suffix element.
suffixClass() Set the class attribute for the suffix element.
suffixTag() Set the tag for the suffix-container element.
If false the suffix tag will be disabled.
template() Set the template for the HTML output.
tokenValues() Set the token values for the HTML output.
widget() Instantiates the TextArea::class.

Validate methods

Refer to the Validate Tests for comprehensive examples.

Method Description
maxLength() Set the maxlength attribute.
minLength() Set the minlength attribute.
required() Set the required attribute.