Fuelphp-advanced-form-programming
FuelPHP-高度なフォームプログラミング
FuelPHPは、FieldsetおよびFieldset_Fieldクラスを通じて高度なフォームプログラミングを提供します。 Fieldset は、フォームを作成するオブジェクト指向の方法を提供します。 モデルを完全にサポートしています。 クライアント側およびサーバー側の検証のサポートも組み込まれています。 本格的なフォームを作成するには、適切なフォームと検証設定でモデルを作成するだけで十分です。 この章では、Fieldsetクラスとそれを使用してフォームを作成する方法について学びましょう。
フィールドセット
Fieldsetは Fieldset_Field オブジェクトのコレクションです。 Fieldset_Fieldは、名、姓などのフォームの個々のエントリを定義します。 検証とともに。 Fieldsetクラスには、フィールドを追加/編集/削除するメソッドがあります。 モデルで定義されたフィールドを識別し、指定されたモデルからフィールドを作成するオプションがあります。 Fieldset は、バックグラウンドでFormクラスとValidationクラスを使用して実際の作業を行います。 Fieldsetクラスの重要なメソッドのいくつかを見てみましょう。
フォージ
*forge* は新しいFieldsetインスタンスを作成します。 それは次の2つのパラメータを持っています-
- $ name -フィールドセットの識別子
- $ config -構成配列。 可能なオプションは_validation_instance_および_form_instanceです。 validation_instance_は_Validation_オブジェクトを持つことができ、_form_instance_はFormオブジェクトを持つことができます。
$employee_form = Fieldset::forge('employee');
実例
*instance* は、以前に作成されたFieldsetインスタンスを識別子で返します。
$employee_form = Fieldset::instance('employee');
get_name
フィールドセットインスタンスの識別子を取得します。
$employee_form = Fieldset::forge('employee');
$name = $employee_form->get_name();
add
*add* は新しいFieldset_Fieldインスタンスを作成し、それを現在のフィールドセットに追加します。 次の4つのパラメーターが含まれています。
- $ name -フィールドの名前
- $ label -フィールドのラベル
- $ attributes -HTMLタグの属性
- $ rules -検証ルール
$employee_field = $employee_form-> add (
'employee_lastname',
'Lastname',
array ('class' => 'pretty_input')
);
//with validation rules
$employee_form->add (
'email', 'E-mail',
array('type' => 'email', 'class' => 'pretty_input'),
array('required', 'valid_email')
);
add_before
add_beforeはaddと似ていますが、新しく作成されたフィールドが追加される前にフィールドを指定する追加のパラメーターが1つある点が異なります。
$employee_form->add_before (
'employee_firstname',
'Firstname',
array ('class' => 'pretty_input'),
array(),
'employee_lastname'
);
削除する
*delete* は、フィールドセットから指定されたフィールドを削除します。
$employee_form->delete('employee_firstname');
フィールド
*field* は、すべてのフィールドまたはフィールドセットから指定されたフィールドを取得します。
$fields = $employee_form->field();
$lastname_field = $employee_form->field('employee_lastname');
造る
*build* は_ $ this-> form()-> build()_のエイリアスです。 フォームのHTMLマークアップを生成します。
$employee_form->build(Uri::create('employee/add'));
有効にする
*enable* は、以前に無効にされていたフィールドを再度有効にします。
$employee_form->enable('employee_firstname');
無効にする
*disable* は、フィールドセット内のフィールドの構築を無効にします。
$employee_form->disable('employee_firstname');
form
*form* は、現在のフィールドセットのFormインスタンスを返します。
$form = employee_form->form();
add_model
add_modelは、モデルのフィールドをフィールドセットに追加します。 次の3つのパラメーターがあります。
- $ class -クラス名
- $ instance -フィールドに値を入力するクラスのインスタンス
- $ method -クラス内のメソッドの名前。 このメソッドは、フィールドセットにフィールドを追加するために使用されます。 Orm \ Modelには必要なメソッドがあります。 デフォルトのメソッド名はset_form_fieldsです。
$employee_form = Fieldset::forge('employee');
$employee_form->add_model('Model_Employee');
住む
*populate* は、モデルインスタンスを使用してfieldsetのフィールドの初期値を設定します。
$emp = new Model_Employee();
$emp->name = "Jon";
$employee_form->populate($emp);
移住する
*repopulate* は、フィールドセットのフィールドに再入力することを除いて、populateと同じです。
検証
*validation* は、現在のフィールドセットの検証インスタンスを取得します。
$validation = $employee_form->validation();
検証済み
$ this→ validation()→ validated()のエイリアス。
入力
$ this→ validation()→ input()のエイリアス。
エラー
$ this→ validation()→ error()のエイリアス。
*show_errors*
$ this→ validation()→ show_errors()のエイリアス。
実施例
Fieldsetクラスを使用してサンプルの従業員アプリケーションに新しい従業員を追加する高度なフォームを作成しましょう。
モデルを更新
必要な検証ルールで従業員モデルを更新し、次のように検証オブザーバーを追加します。
<?php
class Model_Employee extends Orm\Model {
protected static $_connection = 'production';
protected static $_table_name = 'employee';
protected static $_primary_key = array('id');
protected static $_properties = array (
'id',
'name' => array (
'data_type' => 'varchar',
'label' => 'Employee Name',
'validation' => array (
'required',
'min_length' => array(3),
'max_length' => array(80)
),
'form' => array (
'type' => 'text'
),
),
'age' => array (
'data_type' => 'int',
'label' => 'Employee Age',
'validation' => array (
'required',
),
'form' => array ('type' => 'text' ),
),
);
//Just add the Observer, and define the required event
protected static $_observers = array('Orm\\Observer_Validation' => array (
'events' => array('before_save')));
}
ここでは、名前フィールドと年齢フィールドの検証ルールを定義し、モデルをデータベースに保存する前にサーバー側の検証を実行する新しいオブザーバーを追加しました。 同じ検証ルールにより、フォームに必要な入力検証属性も作成されます。
フォームを作成
次のように、Employee Controllerで新しいアクションaction_advancedformを作成します。
public function action_advancedform() {
//create a new fieldset and add employee model
$fieldset = Fieldset::forge('employee')->add_model('Model_Employee');
//get form from fieldset
$form = $fieldset->form();
//add submit button to the form
$form->add('Submit', '', array('type' => 'submit', 'value' => 'Submit'));
//build the form and set the current page as action
$formHtml = $fieldset->build(Uri::create('employee/advancedform'));
//set form in data
$data = array();
$data['form'] = $formHtml;
return Response::forge(View::forge('employee/advancedform', $data, false));
}
ここでは、fieldsetを使用してフォームを作成し、フォームをビューに送信しました。 次に、アクションのビュー fuel/app/views/employee/advancedform.php を次のように追加します。
<!DOCTYPE html>
<html lang = "en">
<head>
<title>Employee :: add page</title>
<meta charset = "utf-8">
<meta name = "viewport" content = "width = device-width, initial-scale = 1">
<?php echo Asset::css('bootstrap.css'); ?>
<style>
table {
width: 90%;
}
table tr {
width: 90%
}
table tr td {
width: 50%
}
input[type = text], select {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type = submit] {
width: 100%;
background-color: #3c3c3c;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 20px;
}
</style>
</head>
<body>
<div class = "container">
<?php
if(isset($errors)) {
echo $errors;
}
echo $form;
?>
</div>
</body>
</html>
これで、ページ http://localhost:8080/employee/add をリクエストすると、次のフォームが表示されます。
プロセスフォーム
アクションメソッド_action_advancedform_を更新してフォームを処理し、ユーザーが入力した従業員データを従業員コントローラーのデータベースに次のように追加します。
public function action_advancedform() {
//create a new fieldset and add employee model
$fieldset = Fieldset::forge('employee')->add_model('Model_Employee');
//get form from fieldset
$form = $fieldset->form();
//add submit button to the form
$form->add('Submit', '', array('type' => 'submit', 'value' => 'Submit'));
//build the form and set the current page as action
$formHtml = $fieldset->build(Uri::create('employee/advancedform'));
if (Input::param() != array()) {
try {
$article = Model_Employee::forge();
$article->name = Input::param('name');
$article->url = Input::param('age');
$article->save();
Response::redirect('employee/list');
}
catch (Orm\ValidationFailed $e) {
$view = View::forge('employee/advancedform');
$view->set('form', $formHtml, false);
$view->set('errors', $e->getMessage(), false);
}
}
return Response::forge($view);
}
ここでは、ユーザーが入力したデータが検証され、データベースに保存されると、従業員リストページにリダイレクトされます。 それ以外の場合は、フォームが再び表示されます。
フォームを作成する
次に、URL http://localhost:8080/employee/add を要求し、従業員データを入力してフォームを送信します。 データが提供されない場合、次のスクリーンショットに示すように、フォームはユーザーにデータの入力を促します。
ユーザーがクライアント側の検証をバイパスした場合、サーバーはフォームを検証し、次のスクリーンショットに示すようにエラーを表示します。
データがクライアントおよびサーバー側の検証に合格した場合、従業員データはデータベースに保存され、ページはリストページにリダイレクトされます。