Cakephp-form-handling

提供:Dev Guides
移動先:案内検索

CakePHP-フォーム処理

CakePHPは、HTMLフォームを簡単かつ安全に処理するためのさまざまな組み込みタグを提供します。 他の多くのPHPフレームワークと同様に、HTMLの主要な要素もCakePHPを使用して生成されます。 以下は、HTML要素を生成するために使用されるさまざまな関数です。

次の関数は、選択オプションを生成するために使用されます。

Syntax selectOptions( array _$elements array(), array $parents array(), boolean $showParents null, array $attributes array() )
Parameters
  • フォーマットする要素
  • OPTGROUPの親
  • 親を表示するかどうか
  • HTML属性
Returns array
Description Returns an array of formatted OPTION/OPTGROUP elements

次の関数は* HTML select要素を生成するために使用されます*。

Syntax select( string $fieldName, array $options array(), array $attributes array() )
Parameters

Name attribute of the SELECT

SELECT要素で使用されるOPTION要素の配列(「値」⇒「テキスト」のペアとして)

select要素のHTML属性。

Returns Formatted SELECT element
Description Returns a formatted SELECT element

次の関数は、HTMLページでボタンを生成するために使用されます*。

Syntax Button (string $title, array $options array() )
Parameters
  • ボタンのキャプション。 自動的にHTMLエンコードされません。 *オプションとHTML属性の配列。
Returns HTML button tag.
Description Creates a* <button> tag. The type attribute defaults to type="submit*". You can change it to a different value by using $options['type'].

以下の関数は、HTMLページでチェックボックスを生成するために使用されます*。

Syntax Checkbox (string $fieldName, array $options array() )
Parameters
  • この「Modelname.fieldname」のようなフィールドの名前
  • HTML属性の配列。 可能なオプションは、value、checked、hiddenField、disabled、defaultです。
Returns An HTML text input element.
Description Creates a checkbox input widget.

次の関数は、HTMLページでフォームを作成するために使用されます。

Syntax create( mixed $model null, array $options array() )
Parameters
  • フォームが定義されているモデル名。 プラグインモデルのプラグイン名を含める必要があります。 e.g. ContactManager.Contact。 配列が渡され、$ options引数が空の場合、配列はオプションとして使用されます。 falseの場合、モデルは使用されません。
  • html属性とオプションの配列。 可能なオプションは、type、action、url、default、onsubmit、inputDefaults、encodingです。
Returns A formatted opening FORM tag.
Description Returns an HTML FORM element.

次の関数は、HTMLページでファイルのアップロード機能を提供するために使用されます。

Syntax file(string $fieldName, array $options array() )
Parameters
  • 「Modelname.fieldname」という形式のフィールドの名前
  • HTML属性の配列。
Returns A generated file input.
Description Creates file input widget.

次の関数は、HTMLページに hidden element を作成するために使用されます。

Syntax hidden( string $fieldName, array $options array() )
Parameters
  • 「Modelname.fieldname」の形式のフィールドの名前
  • HTML属性の配列。
Returns A generated hidden input
Description Creates a hidden input field

次の関数は、HTMLページで* input要素*を生成するために使用されます。

Syntax Input (string $fieldName, array $options array() )
Parameters
  • これは「Modelname.fieldname」でなければなりません
  • 入力の各タイプは異なるオプションを取ります
Returns Completed form widget
Description Generates a form input element complete with label and wrapper div

次の関数は、HTMLページで*ラジオボタン*を生成するために使用されます。

Syntax Radio (string $fieldName, array $options array(), array $attributesarray() )
Parameters
  • この「Modelname.fieldname」のようなフィールドの名前
  • ラジオボタンオプションの配列。
  • HTML属性の配列、および上記の特別な属性。
Returns Completed radio widget set
Description Creates a set of radio widgets. Will create a legend and fieldset by default. Use $options to control this.

次の関数は、HTMLページで submit ボタンを生成するために使用されます。

Syntax Submit (string $caption null, array $options array() )
Parameters
  • ボタンに表示されるラベル、または文字列に://または拡張子.jpg、.jpe、.jpeg、.gif、.pngが含まれる場合、拡張子が存在する場合は画像を使用し、かつ最初の文字が/の場合、画像はwebrootに関連します、または最初の文字が/でない場合、画像はwebroot/imgに相対的です。
  • オプションの配列。 可能なオプションは、div、before、after、typeなどです。
Returns An HTML submit button
Description Creates a submit button element. This method will generate <input/> elements that can be used to submit, and reset forms by using $options. Image submits can be created by supplying an image path for $caption.

次の関数は、HTMLページでtextarea要素を生成するために使用されます*。

Syntax Textarea (string $fieldName, array $options array() )
Parameters
  • 「Modelname.fieldname」という形式のフィールドの名前
  • HTML属性の配列、エスケープなどの特別なオプション
Returns A generated HTML text input element
Description Creates a textarea widget

次のコードに示すように、 config/routes.php ファイルに変更を加えます。

*config/routes.php*
<?php
   use Cake\Core\Plugin;
   use Cake\Routing\RouteBuilder;
   use Cake\Routing\Router;

   Router::defaultRouteClass('DashedRoute');
   Router::scope('/', function (RouteBuilder $routes) {
      $routes->connect('register',['controller'=>'Registrations','action'=>'index']);
      $routes->fallbacks('DashedRoute');
   });
   Plugin::routes();
*src/Controller/RegistrationController.php* に *RegistrationController.php* ファイルを作成します。 次のコードをコントローラーファイルにコピーします。
*src/Controller/RegistrationController.php*
<?php
   namespace App\Controller;
   use App\Controller\AppController;

   class RegistrationsController extends AppController{
      public function index(){
         $country = array('India','United State of America','United Kingdom');
         $this->set('country',$country);
         $gender = array('Male','Female');
         $this->set('gender',$gender);
      }
   }
?>
*src/Template* にディレクトリ *Registrations* を作成し、そのディレクトリの下に *index.ctp* という名前のViewファイルを作成します。 そのファイルに次のコードをコピーします。
*src/Template/Registrations/index.ctp*
<?php
   echo $this->Form->create("Registrations",array('url'=>'/register'));
   echo $this->Form->input('username');
   echo $this->Form->input('password');
   echo $this->Form->input('password');
   echo '<label for="country">Country</label>';
   echo $this->Form->select('country',$country);
   echo '<label for="gender">Gender</label>';
   echo $this->Form->radio('gender',$gender);
   echo '<label for="address">Address</label>';
   echo $this->Form->textarea('address');
   echo $this->Form->file('profilepic');
   echo '<div>'.$this->Form->checkbox('terms').
      '<label for="country">Terms &Conditions</label></div>';
   echo $this->Form->button('Submit');
   echo $this->Form->end();
?>

次のURLにアクセスして上記の例を実行します- http://localhost:85/CakePHP/register

出力

実行すると、次の出力が表示されます。

フォーム処理