LoginSignup
1
0

More than 3 years have passed since last update.

ヘルパーでtextareaのrowsの初期値を変更する

Last updated at Posted at 2019-07-25

はじめに

php
echo $this->Form->textarea('test');

を実行すると

html
<textarea name="test" rows="5"></textarea>

が出力されます。rowsの初期値が5だということがわかります。

php
echo $this->Form->textarea('test',['rows'=>'10']);

を実行することで

html
<textarea name="test" rows="10"></textarea>

のようにrowsの値を変更する事はできますが、「ほとんどどのtextareaもrowsは10で使うんだよな~」、「rowsは指定しないでcssで高さ調整したいんだよな~」という方は初期値をカスタムした方が使いやすくなりそうです。

rowsの初期値を変更する

vendor/cakephp/cakephp/src/View/Widget/TextareaWidget.php
$data += [
    'val' => '', 
    'name' => '', 
    'escape' => true,
    'rows' => 5,
    'templateVars' => []
];

'rows' => 5,'rows' => 10,に書き換えます。もう一度

php
echo $this->Form->textarea('test');

を実行すると

html
<textarea name="test" rows="10"></textarea>

が出力されるようになりました。

オプションがない時はrowsを出力しない

vendor/cakephp/cakephp/src/View/Widget/TextareaWidget.php
$data += [
    'val' => '', 
    'name' => '', 
    'escape' => true,
    'rows' => 5,
    'templateVars' => []
];

'rows' => 5,を消しちゃいます。もう一度

php
echo $this->Form->textarea('test');

を実行すると

html
<textarea name="test"></textarea>

が出力されるようになりました。

リンク

Form - 3.8
ヘルパーでtextareaのrowsの初期値を変更する - Qiita

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0