NEW ENTRIES
CATEGORIES
ARCHIVES

次にジャンプする場合のルールの使用についてです。

addRuleで定義されたルールを利用できるのでしょうか?
HTML_QuickFormを使う一つの理由が、このaddRuleだったりします。


ということなのですが、実は私はHTML_QuickFormを使う時、ルールは使ってません。
ルールがHTML_QuickFormの最大の魅力のと思っておられる方が多いようなのですが、
なぜか私はルールに魅力を感じないんです。ルールってそんなに便利ですか???

でもせっかくなのでジャンプしてなおかつルールを使用する方法を確認してみました。
ルールの魅力がわかっていない私が確認した方法なので、ルールを愛用されている方
が見れば「これは違う!」と思われるかもしれませんが。。。

1ページ目が入力画面、2ページが確認画面、3ページが完了画面というよくある流れ
のサンプルです。長くなりますが、ソースをそのまま書いてしまいます。
(ブログにソースを載せる時の常識ってあるんでしょうか?Webアプリも初心者なら、
ブログはもっと初心者なもんで。。。ソースが字下げされないし、テンプレートは一部
表示が変です。回避方法がわかり次第修正します。)


<?php
////////////////////////////////////////////////////////////////////////////////
// ruleSample.php
// 2005.12.16 作成
////////////////////////////////////////////////////////////////////////////////

require_once './commonTips.php';

// 1ページ目
class FirstPage extends HTML_QuickForm_Page {
function buildForm() {
$this->_formBuilt = true;
$this->addElement('text',
'txtName',
''
);
$this->addRule('txtName', '必須項目です。', 'required');
$this->addElement('submit',
$this->getButtonName('AShowSecond'),
'送信');
$this->setDefaultAction('next');
}
}

// 2ページ目
class SecondPage extends HTML_QuickForm_Page {
function buildForm() {
$this->_formBuilt = true;
$this->addElement('static', 'lblName', '',
$_SESSION['inputName']);
$this->addElement('submit',
$this->getButtonName('AShowFirst'),
'戻る');
$this->addElement('submit',
$this->getButtonName('AShowLast'),
'送信');
$this->setDefaultAction('next');
}
}

// 3ページ目
class LastPage extends HTML_QuickForm_Page {
function buildForm() {
$this->_formBuilt = true;
$this->addElement('submit',
$this->getButtonName('AShowFirst'),
'終了');
$this->addElement('submit',
$this->getButtonName('AShowSecond'),
'戻る');
$this->setDefaultAction('next');
}
}

// 1ページを表示
class ActionShowFirst extends HTML_QuickForm_Action {
function perform(&$page, $actionName)
{
$page->isFormBuilt() or $page->buildForm();
$next =& $page->controller->getPage('PRuleFirst');
$next->handle('jump');
}
}

// 2ページを表示
class ActionShowSecond extends HTML_QuickForm_Action {
function perform(&$page, $actionName)
{
$page->isFormBuilt() or $page->buildForm();
if ($page->validate()) {
$pageName = $page->getAttribute('id');
if ($pageName == 'PRuleFirst') {
$data =& $page->controller->container();
$data['values'][$pageName] = $page->exportValues();
$_SESSION['inputName'] = $data['values'][$pageName]['txtName'];
}
$next =& $page->controller->getPage('PRuleSecond');
$next->handle('jump');
} else {
$page->handle('display');
}
}
}

// 3ページを表示
class ActionShowLast extends HTML_QuickForm_Action {
function perform(&$page, $actionName)
{
$page->isFormBuilt() or $page->buildForm();
$next =& $page->controller->getPage('PRuleLast');
$next->handle('jump');
}
}

class ActionDisplay extends HTML_QuickForm_Action_Display {
function _renderForm(&$page) {

$smartyObj = new tipsSmarty();

$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($smartyObj);
$page->accept($renderer);
$smartyObj->assign('page', $renderer->toArray());
$page_action = $page->controller->getActionName();
$smartyObj->tipsDisplay($page_action[0]);
}
}

session_start();

$controller =& new HTML_QuickForm_Controller('form', false);
// 必要なページをすべて追加
$controller->addPage(new FirstPage('PRuleFirst'));
$controller->addPage(new SecondPage('PRuleSecond'));
$controller->addPage(new LastPage('PRuleLast'));
// ハンドラ設定
$controller->addAction('jump', new HTML_QuickForm_Action_Jump());
$controller->addAction('display', new ActionDisplay());
$controller->addAction('AShowFirst', new ActionShowFirst());
$controller->addAction('AShowSecond', new ActionShowSecond());
$controller->addAction('AShowLast', new ActionShowLast());
// 実行
$controller->run();
?>


commonTips.phpは勉強会ページで説明しているものと全く同じです。
まず、ruleSample.phpを作ります。
2ページ目を表示するためのアクション、ActionShowSecondで入力チェックをしてます。
入力が正しければ、2ページ目を表示、不正ならジャンプ前のページのままです。
今回は「1ページ目から2ページ目へ」と「3ページ目から2ページ目へ戻る」は同じ
アクションを使っているので、飛び元のページ名を取得して処理を分けています。
元々それぞれ別のアクションにしておくというのも一つの方法だと思います。

あと、テンプレートを作ります。2,3ページ目は特に目新しいことはないので省略
して、1ページ目PRuleFirst.tpl.htmlのみ載せておきます。
エラーメッセージを表示するため{$page.txtName.error}を追加してます。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>HTML_QuickFormController-Tips</title>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-JP">
</head>
<body bgcolor="#99ccff">
<form {$page.attributes}>
</table>
<table width="100%" border="0">
<tr>
<td align="center">
名前を入力してください。


</td>
</tr>
<tr>
<td align="center">
{$page.txtName.error}
{$page.txtName.html}


</td>
</tr>
<tr>
<td align="center">
<!-- ボタンたち -->
{$page._qf_PRuleFirst_AShowSecond.html}
</td>
</tr>
</table>
</form>
</body>
</html>

2005,12,16, Fri 12:45
たぬきと一緒にお勉強::HTML_QuickForm_Controllercomments (0) trackback (x)

先日、たぬきの勉強会ページ「第8回 HTML_QuickForm_Controller大好き!」
に質問のメールをいただきました。(読んでくれている方がいるとわかるとうれしい
ですね。)質問と回答を勉強会ページの補足としてここにまとめておきます。

まず1点目

HTML_QuickForm_Controllerのサンプルですと、なにげにサンプルではコントローラー
をnon-modalモードにしていますが、これですとページがフォームですとエラーチェック
をしないのですね? 逆にmodalですと、自由にページをジャンプできないのですね?
私はここではまってしまいました。


ということです。そうなんです。私もはまったので、説明を書くべきだと思ったのですが
HTML_QuickForm_Controllerのコンストラクタの第二引数「フォームがモーダルかどうか」
という言葉の意味が、説明できるほどに分からなくて省略してしまいました。とにかく
「ジャンプしたければ第二引数をfalseに」
というのは間違いありません。
$controller =& new HTML_QuickForm_Controller('form', false);です。

2005,12,16, Fri 12:38
たぬきと一緒にお勉強::HTML_QuickForm_Controller 】 comments (x) trackback (x)
  PAGETOP  
CALENDAR
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30     
<< 2024 April >>
PROFILE
LOGIN
現在のモード: ゲストモード
USER ID:
PASSWORD:
POWERED BY
Script by:Blogn+(ぶろぐん+)
Skin by:vivid*face
RSS 1.0
処理時間 0.025281秒
SEARCH