Unverified Commit 2e9f8215 authored by larowlan's avatar larowlan

Issue #2594553 by gambry, arshadcn, Sagar Ramgade, Yogesh Pawar, garnett2125,...

Issue #2594553 by gambry, arshadcn, Sagar Ramgade, Yogesh Pawar, garnett2125, larowlan, lauriii: Add #maxlength to textarea in Form API
parent 04573c06
......@@ -370,12 +370,13 @@ function template_preprocess_form(&$variables) {
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #description, #rows, #cols,
* #placeholder, #required, #attributes, #resizable
* Properties used: #title, #value, #description, #rows, #cols, #maxlength,
* #placeholder, #required, #attributes, #resizable.
*/
function template_preprocess_textarea(&$variables) {
$element = $variables['element'];
Element::setAttributes($element, ['id', 'name', 'rows', 'cols', 'placeholder']);
$attributes = ['id', 'name', 'rows', 'cols', 'maxlength', 'placeholder'];
Element::setAttributes($element, $attributes);
RenderElement::setAttributes($element, ['form-textarea']);
$variables['wrapper_attributes'] = new Attribute();
$variables['attributes'] = new Attribute($element['#attributes']);
......
......@@ -12,6 +12,7 @@
* - #cols: Number of columns in the text box.
* - #resizable: Controls whether the text area is resizable. Allowed values
* are "none", "vertical", "horizontal", or "both" (defaults to "vertical").
* - #maxlength: The maximum amount of characters to accept as input.
*
* Usage example:
* @code
......
<?php
namespace Drupal\Tests\system\Kernel\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\KernelTests\KernelTestBase;
use Symfony\Component\CssSelector\CssSelectorConverter;
/**
* Tests the maxlength HTML attribute on form elements.
*
* @group Form
*/
class FormElementMaxlengthTest extends KernelTestBase implements FormInterface {
/**
* {@inheritdoc}
*/
public static $modules = ['system'];
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'form_test_maxlength';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['title'] = [
'#type' => 'textfield',
'#maxlength' => 255,
];
$form['description'] = [
'#type' => 'textarea',
'#maxlength' => 255,
];
$form['submit'] = [
'#type' => 'submit',
'#value' => 'Submit',
];
return $form;
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {}
/**
* Ensures maxlength attribute can be used in compatible elements.
*/
public function testAttributes() {
/** @var \Drupal\Core\Form\FormBuilderInterface $form_builder */
$form_builder = $this->container->get('form_builder');
$form_state = new FormState();
$elements = $form_builder->buildForm($this, $form_state);
$this->render($elements);
$css_selector_converter = new CssSelectorConverter();
$elements = $this->xpath($css_selector_converter->toXPath('input[name=title][maxlength=255]'));
$this->assertCount(1, $elements, 'Text field has correct maxlength in form.');
$elements = $this->xpath($css_selector_converter->toXPath('textarea[name=description][maxlength=255]'));
$this->assertCount(1, $elements, 'Textarea field has correct maxlength in form.');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment