Skip to content

Commit 2d5aa6b

Browse files
committed
Reload TextareaWidget
1 parent 38b0c28 commit 2d5aa6b

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

plugin.manifest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
'className' => 'Backend.Form',
4747
'widgets' => [
4848
'file' => ['Backend\View\Widget\FileWidget'],
49+
'textarea' => ['Backend\View\Widget\TextareaWidget'],
4950
'checkbox' => ['Backend\View\Widget\CheckboxWidget']
5051
]
5152
]);

src/View/Widget/TextareaWidget.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
/**
3+
* CakeCMS Backend
4+
*
5+
* This file is part of the of the simple cms based on CakePHP 3.
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*
9+
* @package Backend
10+
* @license MIT
11+
* @copyright MIT License http://www.opensource.org/licenses/mit-license.php
12+
* @link https://github.com/CakeCMS/Backend".
13+
* @author Sergey Kalistratov <kalistratov.s.m@gmail.com>
14+
*/
15+
16+
namespace Backend\View\Widget;
17+
18+
use JBZoo\Utils\Str;
19+
use Cake\View\Form\ContextInterface;
20+
use Cake\View\Widget\TextareaWidget as CakeTextareaWidget;
21+
22+
/**
23+
* Class TextareaWidget
24+
*
25+
* @package Backend\View\Widget
26+
*/
27+
class TextareaWidget extends CakeTextareaWidget
28+
{
29+
30+
/**
31+
* Render a text area form widget.
32+
*
33+
* Data supports the following keys:
34+
*
35+
* - `name` - Set the input name.
36+
* - `val` - A string of the option to mark as selected.
37+
* - `escape` - Set to false to disable HTML escaping.
38+
*
39+
* All other keys will be converted into HTML attributes.
40+
*
41+
* @param array $data The data to build a textarea with.
42+
* @param \Cake\View\Form\ContextInterface $context The current form context.
43+
* @return string HTML elements.
44+
*/
45+
public function render(array $data, ContextInterface $context)
46+
{
47+
$data = array_merge(['class' => null], $data);
48+
49+
$data['class'] .= ' materialize-textarea';
50+
$data['class'] = Str::trim($data['class']);
51+
52+
return parent::render($data, $context);
53+
}
54+
}

tests/TestCase/View/Helper/FormHelperTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,41 @@ public function testInput()
9595
];
9696

9797
$this->assertHtml($expected, $this->Form->control('test'));
98+
99+
$expected = [
100+
'div' => ['class' => 'input-field textarea'],
101+
'textarea' => [
102+
'rows' => 5,
103+
'name' => 'test',
104+
'id' => 'test',
105+
'class' => 'materialize-textarea',
106+
],
107+
'/textarea',
108+
'label' => ['for' => 'test'],
109+
'Test',
110+
'/label',
111+
'/div'
112+
];
113+
$this->assertHtml($expected, $this->Form->control('test', ['type' => 'textarea']));
114+
115+
$expected = [
116+
'div' => ['class' => 'input-field textarea'],
117+
'textarea' => [
118+
'rows' => 5,
119+
'name' => 'test',
120+
'id' => 'test',
121+
'class' => 'custom-class materialize-textarea'
122+
],
123+
'/textarea',
124+
'label' => ['for' => 'test'],
125+
'Test',
126+
'/label',
127+
'/div'
128+
];
129+
$this->assertHtml($expected, $this->Form->control('test', [
130+
'type' => 'textarea',
131+
'class' => 'custom-class'
132+
]));
98133
}
99134

100135
public function testSwitcher()

0 commit comments

Comments
 (0)