[FEATURE] Add readonly and required attributes to TextareaViewHelper 28/54328/11
authorRaphael Graf <graf@netvertising.ch>
Sun, 8 Oct 2017 18:17:09 +0000 (20:17 +0200)
committerBenni Mack <benni@typo3.org>
Sun, 11 Feb 2018 00:23:19 +0000 (01:23 +0100)
The f:form.textarea view helper now supports the HTML attributes
`readonly` and `required`.

Resolves: #82704
Releases: master
Change-Id: I890f8551fb5eccd30dedd2ad8185aa9e9a24c86e
Reviewed-on: https://review.typo3.org/54328
Tested-by: TYPO3com <no-reply@typo3.com>
Reviewed-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Tested-by: Mathias Schreiber <mathias.schreiber@typo3.com>
Reviewed-by: Benni Mack <benni@typo3.org>
Tested-by: Benni Mack <benni@typo3.org>
typo3/sysext/core/Documentation/Changelog/master/Feature-82704-AddReadonlyAndRequiredAttributesToTextareaViewHelper.rst [new file with mode: 0644]
typo3/sysext/fluid/Classes/ViewHelpers/Form/TextareaViewHelper.php
typo3/sysext/fluid/Tests/Unit/ViewHelpers/Form/TextareaViewHelperTest.php

diff --git a/typo3/sysext/core/Documentation/Changelog/master/Feature-82704-AddReadonlyAndRequiredAttributesToTextareaViewHelper.rst b/typo3/sysext/core/Documentation/Changelog/master/Feature-82704-AddReadonlyAndRequiredAttributesToTextareaViewHelper.rst
new file mode 100644 (file)
index 0000000..61ab5a6
--- /dev/null
@@ -0,0 +1,30 @@
+.. include:: ../../Includes.txt
+
+============================================================================
+Feature: #82704 - Add readonly and required attributes to TextareaViewHelper
+============================================================================
+
+See :issue:`82704`
+
+Description
+===========
+
+The view helper `f:form.textarea` now supports the field attributes `readonly` and `required`.
+
+
+Impact
+======
+
+The attributes `readonly` and `required` may be set by using the `f:form.textarea` view helper.
+
+Example:
+
+.. code-block:: html
+
+       <!-- Set required attribute -->
+       <f:form.textarea name="foobar" required="1" />
+
+       <!-- Set readonly attribute -->
+       <f:form.textarea name="foobar" readonly="1" />
+
+.. index:: Fluid, NotScanned
index 83b1513..436b6fc 100644 (file)
@@ -50,6 +50,8 @@ class TextareaViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormF
         $this->registerTagAttribute('disabled', 'string', 'Specifies that the input element should be disabled when the page loads');
         $this->registerTagAttribute('placeholder', 'string', 'The placeholder of the textarea');
         $this->registerArgument('errorClass', 'string', 'CSS class to set if there are errors for this view helper', false, 'f3-form-error');
+        $this->registerTagAttribute('readonly', 'string', 'The readonly attribute of the textarea', false);
+        $this->registerArgument('required', 'bool', 'Specifies whether the textarea is required', false, false);
         $this->registerUniversalTagAttributes();
     }
 
@@ -61,14 +63,17 @@ class TextareaViewHelper extends \TYPO3\CMS\Fluid\ViewHelpers\Form\AbstractFormF
      */
     public function render()
     {
+        $required = $this->arguments['required'];
         $name = $this->getName();
         $this->registerFieldNameForFormTokenGeneration($name);
         $this->setRespectSubmittedDataValue(true);
 
         $this->tag->forceClosingTag(true);
         $this->tag->addAttribute('name', $name);
+        if ($required === true) {
+            $this->tag->addAttribute('required', 'required');
+        }
         $this->tag->setContent(htmlspecialchars($this->getValueAttribute()));
-
         $this->addAdditionalIdentityPropertiesIfNeeded();
         $this->setErrorClassAttribute();
 
index e769ac7..01b6c46 100644 (file)
@@ -53,24 +53,16 @@ class TextareaViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
      */
     public function renderCorrectlySetsNameAttributeAndContent()
     {
-        $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
-            ->setMethods(['addAttribute', 'setContent', 'render'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'NameOfTextarea');
-        $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('NameOfTextarea');
-        $mockTagBuilder->expects($this->once())->method('setContent')->with('Current value');
-        $mockTagBuilder->expects($this->once())->method('render');
-        $this->viewHelper->_set('tag', $mockTagBuilder);
-
         $arguments = [
             'name' => 'NameOfTextarea',
             'value' => 'Current value'
         ];
         $this->viewHelper->setArguments($arguments);
 
-        $this->viewHelper->setViewHelperNode(new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\EmptySyntaxTreeNode());
-        $this->viewHelper->initializeArgumentsAndRender();
+        $this->viewHelper->setViewHelperNode(new Fixtures\EmptySyntaxTreeNode());
+        $actual = $this->viewHelper->initializeArgumentsAndRender();
+        $expected = '<textarea name="NameOfTextarea">Current value</textarea>';
+        $this->assertSame($expected, $actual);
     }
 
     /**
@@ -87,24 +79,16 @@ class TextareaViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
      */
     public function renderEscapesTextareaContent()
     {
-        $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
-            ->setMethods(['addAttribute', 'setContent', 'render'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $mockTagBuilder->expects($this->once())->method('addAttribute')->with('name', 'NameOfTextarea');
-        $this->viewHelper->expects($this->once())->method('registerFieldNameForFormTokenGeneration')->with('NameOfTextarea');
-        $mockTagBuilder->expects($this->once())->method('setContent')->with('some &lt;tag&gt; &amp; &quot;quotes&quot;');
-        $mockTagBuilder->expects($this->once())->method('render');
-        $this->viewHelper->setTagBuilder($mockTagBuilder);
-
         $arguments = [
             'name' => 'NameOfTextarea',
             'value' => 'some <tag> & "quotes"'
         ];
         $this->viewHelper->setArguments($arguments);
 
-        $this->viewHelper->setViewHelperNode(new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\EmptySyntaxTreeNode());
-        $this->viewHelper->initializeArgumentsAndRender();
+        $this->viewHelper->setViewHelperNode(new Fixtures\EmptySyntaxTreeNode());
+        $actual = $this->viewHelper->initializeArgumentsAndRender();
+        $expected = '<textarea name="NameOfTextarea">some &lt;tag&gt; &amp; &quot;quotes&quot;</textarea>';
+        $this->assertSame($expected, $actual);
     }
 
     /**
@@ -112,22 +96,49 @@ class TextareaViewHelperTest extends \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\For
      */
     public function renderAddsPlaceholder()
     {
-        $mockTagBuilder = $this->getMockBuilder(TagBuilder::class)
-            ->setMethods(['addAttribute', 'setContent', 'render'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $mockTagBuilder->expects($this->at(0))->method('addAttribute')->with('placeholder', 'SomePlaceholder');
-        $mockTagBuilder->expects($this->at(1))->method('addAttribute')->with('name', 'NameOfTextarea');
-        $mockTagBuilder->expects($this->once())->method('render');
-        $this->viewHelper->setTagBuilder($mockTagBuilder);
-
         $arguments = [
             'name' => 'NameOfTextarea',
             'placeholder' => 'SomePlaceholder'
         ];
         $this->viewHelper->setArguments($arguments);
 
-        $this->viewHelper->setViewHelperNode(new \TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Form\Fixtures\EmptySyntaxTreeNode());
-        $this->viewHelper->initializeArgumentsAndRender();
+        $this->viewHelper->setViewHelperNode(new Fixtures\EmptySyntaxTreeNode());
+        $actual = $this->viewHelper->initializeArgumentsAndRender();
+        $expected = '<textarea placeholder="SomePlaceholder" name="NameOfTextarea"></textarea>';
+        $this->assertSame($expected, $actual);
+    }
+
+    /**
+     * @test
+     */
+    public function renderAddsReadonly()
+    {
+        $arguments = [
+            'readonly' => 'foo',
+            'name' => 'NameOfTextarea',
+        ];
+        $this->viewHelper->setArguments($arguments);
+
+        $this->viewHelper->setViewHelperNode(new Fixtures\EmptySyntaxTreeNode());
+        $actual = $this->viewHelper->initializeArgumentsAndRender();
+        $expected = '<textarea readonly="foo" name="NameOfTextarea"></textarea>';
+        $this->assertSame($expected, $actual);
+    }
+
+    /**
+     * @test
+     */
+    public function renderAddsRequired()
+    {
+        $arguments = [
+            'required' => true,
+            'name' => 'NameOfTextarea',
+        ];
+        $this->viewHelper->setArguments($arguments);
+
+        $this->viewHelper->setViewHelperNode(new Fixtures\EmptySyntaxTreeNode());
+        $actual = $this->viewHelper->initializeArgumentsAndRender();
+        $expected = '<textarea name="NameOfTextarea" required="required"></textarea>';
+        $this->assertSame($expected, $actual);
     }
 }