質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.31%
CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

解決済

2回答

5406閲覧

【CakePHP3】フォームヘルパーで生成されるradioのidを指定したい

h-yonemu

総合スコア11

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2018/10/11 13:45

CakePHP2.xからCakePHP3.xにバージョンアップしていますが、フォームヘルパーで出力されるidが指定しているにも関わらず違うものが設定されています。指定したidを設定する方法はありますか?
JavaScriptを使用していろいろと操作をしているので大変困っています。

【cakePHP3のフォームヘルパーで設定したコード】

CakePHP

1<?php echo $this->Form->input('rdoZaishokuKbn1', ['type' => 'radio', 'id' => 'rdoZaishokuKbn11', 'name' => 'rdoZaishokuKbn1', 'options' => array('1' => ''), 'onclick' => 'shokumuKikanListCtl(1);', 'label' => false, 'templates' => ['inputContainer' => '{{content}}', 'inputContainerError' => '{{content}{error}}'], 'hiddenField' => false]); ?>在職中&nbsp; 2<?php echo $this->Form->input('rdoZaishokuKbn1', ['type' => 'radio', 'id' => 'rdoZaishokuKbn01', 'name' => 'rdoZaishokuKbn1', 'options' => array('2' => ''), 'onclick' => 'shokumuKikanListCtl(1);', 'label' => false, 'templates' => ['inputContainer' => '{{content}}', 'inputContainerError' => '{{content}{error}}'], 'hiddenField' => false]); ?>離職中

出力されたコード(CakePHP3.x)

HTML

1<label for="rdozaishokukbn1-1"> 2 <input name="rdoZaishokuKbn1" disabled="" id="rdozaishokukbn1-1" onclick="shokumuKikanListCtl(1);" type="radio" value="1"> 3</label> 4在職中 5<label for="rdozaishokukbn1-2"> 6 <input name="rdoZaishokuKbn1" disabled="" id="rdozaishokukbn1-2" onclick="shokumuKikanListCtl(1);" type="radio" value="2"> 7</label> 8離職中

ちなみなに、バージョンアップ前の出力されたコード(CakePHP2.x)は

HTML

1<input name="rdoZaishokuKbn1" disabled="" id="rdoZaishokuKbn11" onclick="shokumuKikanListCtl(1);" type="radio" value="1"> 2在職中 3<input name="rdoZaishokuKbn1" disabled="" id="rdoZaishokuKbn01" onclick="shokumuKikanListCtl(1);" type="radio" value="2"> 4離職中

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

ベストアンサー

以下のようにやればできそうでした

<?php echo $this->Form->input('rdoZaishokuKbn1', ['type' => 'radio', 'name' => 'rdoZaishokuKbn1', 'options' => [['value' => '1', 'text' => '', 'id' => 'rdoZaishokuKbn11']], 'onclick' => 'shokumuKikanListCtl(1);', 'label' => false, 'templates' => ['inputContainer' => '{{content}}', 'inputContainerError' => '{{content}{error}}'], 'hiddenField' => false]); ?>在職中&nbsp; <?php echo $this->Form->input('rdoZaishokuKbn1', ['type' => 'radio', 'name' => 'rdoZaishokuKbn1', 'options' => [['value' => '2', 'text' => '', 'id' => 'rdoZaishokuKbn01']], 'onclick' => 'shokumuKikanListCtl(1);', 'label' => false, 'templates' => ['inputContainer' => '{{content}}', 'inputContainerError' => '{{content}{error}}'], 'hiddenField' => false]); ?>離職中

複数のラジオボタンを表示可能なのでoptions側でidを指定する必要があるみたいです。たぶん、「在籍中」と「離職中」を1つのinputメソッドで出力させることもできると思います。
マニュアルにも関連する情報はあったので参照してください

投稿2018/10/12 03:48

編集2018/10/12 03:53
popobot

総合スコア6586

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2018/10/12 03:53

それやって出来なかったっていってる
popobot

2018/10/12 03:55 編集

差分がわかりにくいですが、以下の部分が肝です 'options' => [['value' => '1', 'text' => '', 'id' => 'rdoZaishokuKbn11']]
h-yonemu

2018/10/13 02:26

回答ありがとうございます。 教えて頂いた通りにやってみるとうまくいきました。これでJavaScriptを変更しなくても大丈夫になりました。 ありがとうございます。
guest

0

誤答くさいので参考までに

Cake\View\Helper\FormHelper

$options += ['id' => $this->_domId($fieldName)];`

は確か id は指定できないことを示しておりIDジェネレーターは

Cake\View\Helper\IdGeneratorTrait

/** * Generate an ID suitable for use in an ID attribute. * * @param string $value The value to convert into an ID. * @return string The generated id. */ protected function _domId($value) { $domId = mb_strtolower(Text::slug($value, '-')); if ($this->_idPrefix) { $domId = $this->_idPrefix . '-' . $domId; } return $domId; }

となっており

Cake\Utility\Text

/** * Returns a string with all spaces converted to dashes (by default), * characters transliterated to ASCII characters, and non word characters removed. * * ### Options: * * - `replacement`: Replacement string. Default '-'. * - `transliteratorId`: A valid tranliterator id string. * If default `null` Text::$_defaultTransliteratorId to be used. * If `false` no transliteration will be done, only non words will be removed. * - `preserve`: Specific non-word character to preserve. Default `null`. * For e.g. this option can be set to '.' to generate clean file names. * * @param string $string the string you want to slug * @param array $options If string it will be use as replacement character * or an array of options. * @return string */ public static function slug($string, $options = []) { if (is_string($options)) { $options = ['replacement' => $options]; } $options += [ 'replacement' => '-', 'transliteratorId' => null, 'preserve' => null ]; if ($options['transliteratorId'] !== false) { $string = static::transliterate($string, $options['transliteratorId']); } $regex = '^\s\p{Ll}\p{Lm}\p{Lo}\p{Lt}\p{Lu}\p{Nd}'; if ($options['preserve']) { $regex .= preg_quote($options['preserve'], '/'); } $quotedReplacement = preg_quote($options['replacement'], '/'); $map = [ '/[' . $regex . ']/mu' => ' ', '/[\s]+/mu' => $options['replacement'], sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '', ]; $string = preg_replace(array_keys($map), $map, $string); return $string; }

と - は使用不可なので

FormHelper を自作してください。

投稿2018/10/12 00:24

編集2018/10/12 04:00
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

popobot

2018/10/12 03:49

> $options += ['id' => $this->_domId($fieldName)]; これは、$optionsに`id`が未指定だった場合のデフォルト値ですね。指定されていれば、この値は使われません。
退会済みユーザー

退会済みユーザー

2018/10/12 04:00

先勝ちだっけ? いまいち配列の足し算とか不思議ちゃんは・・・
h-yonemu

2018/10/13 02:31

回答ありがとうございます。 FormHelperは自作しなかったですが、参考になりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.31%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問