Cakephp-form-handling
CakePHP-フォーム処理
CakePHPは、HTMLフォームを簡単かつ安全に処理するためのさまざまな組み込みタグを提供します。 他の多くのPHPフレームワークと同様に、HTMLの主要な要素もCakePHPを使用して生成されます。 以下は、HTML要素を生成するために使用されるさまざまな関数です。
次の関数は、選択オプションを生成するために使用されます。
Syntax | selectOptions( array _$elements array(), array $parents array(), boolean $showParents null, array $attributes array() ) |
Parameters |
|
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 |
|
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 |
|
Returns | An HTML text input element. |
Description | Creates a checkbox input widget. |
次の関数は、HTMLページでフォームを作成するために使用されます。
Syntax | create( mixed $model null, array $options array() ) |
Parameters |
|
Returns | A formatted opening FORM tag. |
Description | Returns an HTML FORM element. |
次の関数は、HTMLページでファイルのアップロード機能を提供するために使用されます。
Syntax | file(string $fieldName, array $options array() ) |
Parameters |
|
Returns | A generated file input. |
Description | Creates file input widget. |
次の関数は、HTMLページに hidden element を作成するために使用されます。
Syntax | hidden( string $fieldName, array $options array() ) |
Parameters |
|
Returns | A generated hidden input |
Description | Creates a hidden input field |
次の関数は、HTMLページで* input要素*を生成するために使用されます。
Syntax | Input (string $fieldName, array $options array() ) |
Parameters |
|
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 |
|
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 |
|
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 |
|
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
出力
実行すると、次の出力が表示されます。