$ this ->form-> set ( 'add-comments' )
->input( 'subject' ,array( 'validation' => 'required|max_length[80]' ))
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' ))
->buttons( 'send' );
if ($result = $ this ->form->result()){
if ($ this ->form->save( 'comments' ,$result)){
redirect( '/node_url' );
}
}
$ this ->form->compile();
* This source code was highlighted with Source Code Highlighter .
最もシンプルでありながら非常に効果的な作業形態を上に示します。
すべての答えの1つのコード
データキャプチャ用に個別のコントローラーメソッドを作成する必要はありません。すべての機能は1つのコードによって引き継がれます。
まず、フォームのidを設定します。これは、フックなどを使用するときに役立ちます。
$ this ->form-> set ( 'add-comments' )
* This source code was highlighted with Source Code Highlighter .
この後、チェーンは要素とそのパラメーター、およびフォームのボタンを設定します。
// ""
->input( 'subject' , array( 'validation' => 'required|max_length[80]' ))
// ""
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' ))
//
->buttons( 'send' );
* This source code was highlighted with Source Code Highlighter .
すぐに可能な質問への答えのための小さな余談:
1.出力を生成するとき、指定されたまたは一般的な翻訳変数に基づいて要素のラベルが取得されます(一般的な変数は編集セクションに保存されます )。
フィールドの名前と説明を含む言語ファイルがあるとします。
[my_form]
subject = " "
subject_description = " . 80 ."
body = ""
body_description = " – 5 , ."
// - i18n
d( 'my_form' );
$ this ->form-> set ( 'add-comments' )
->input( 'subject' , array( 'validation' => 'required|max_length[80]' ))
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' ))
->buttons( 'send' );
if ($result = $ this ->form->result()){
if ($ this ->form->save( 'comments' ,$result)){
redirect( '/node_url' );
}
}
$ this ->form->compile();
* This source code was highlighted with Source Code Highlighter .
フォームの出力の前に現在の翻訳のセクションを設定すると、出力では次の形式のフォームが得られます。
2.エラー処理は、これらのルールに基づいて自動的に行われます。 これ以上のアクションは必要ありません。
エラー処理
フォームを送信すると、検証に失敗すると、エラーが表示された新しい方法でフォームが表示されます。
送信する前にJavaScript検証を追加することにより、フォームを改良できます。
// - i18n
d( 'my_form' );
$ this ->form-> set ( 'add-comments' )
->input( 'subject' , array( 'validation' => 'required|max_length[80]' , 'js_validation' => 'required|length[5,80]' ))
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' , 'js_validation' => 'required|length[5,-1]' ))
->buttons( 'send' );
if ($result = $ this ->form->result()){
if ($ this ->form->save( 'comments' ,$result)){
redirect( '/node_url' );
}
}
$ this ->form->compile();
* This source code was highlighted with Source Code Highlighter .
ページを更新すると、スクリプトはフォームに正しく入力するまでフォームを送信できなくなります。
検証前と検証後のルールが異なる理由をすでに疑問に思っているなら、私は答えます:
-事前検証のために、変更されたMooTools.Floor Form Checkクラスが使用されます。
-検証後の場合、変更されたCodeIgniter ライブラリが使用されます(これはエンジンの基礎です)。
最後に、トピックの作成/編集を簡略化した、より詳細な例を示します。
…
class Index extends Controller{
…
/**
* .
*
* @param int $id id
* @return void
*/
function createdit($id = FALSE){
// - i18n
d( 'node_edit' );
// ,
if ($id && $node = $ this ->db->get_where( 'nodes' ,array( 'id' =>$id))->row()){
/*
*
*
* …
* edit = " '%s'"
* …
*/
title(t( 'edit' ,$node->name));
}
else {
//
title(t( 'node_edit create' ));
}
//
$ this ->form-> set ( 'node-createdit' )
// ""
->input( 'subject' , array( 'validation' => 'required|max_length[80]' , 'js_validation' => 'required|length[-1,80]' ))
// ""
->editor( 'body' ,array( 'validation' => 'required|min_length[5]' , 'js_validation' => 'required|length[5,-1]' ))
//
// , "" "".
->buttons(empty($node) ? 'create' : 'save' );
// —
if (!empty($node)){
$ this ->form->set_values($node);
}
//
if ($result = $ this ->form->result()){
// —
if (!empty($node) && $ this ->form->update( 'nodes' ,$result,array( 'id' =>$node->id)){
redirect( '/nodes/' .$node->id);
}
//
elseif($ this ->form->save( 'comments' ,$result)){
redirect( '/' .$ this ->form->insert_id);
}
}
//
$ this ->form->compile();
}
…
}
* This source code was highlighted with Source Code Highlighter .
もちろん、このトピックでは、 歯車のフォームを操作する最も簡単な例を示します-知覚と明確さを簡単にするために。
フォームを使用した作業の実装は、過去1年間で十分に実証されています。これにより、情報の入力と処理のための一般的なタスクの開発に必要な時間を最小限に抑えることができます。 はい、一部では完全に見えないかもしれませんが、
あなたがあなた自身のアイデアへの短い余談を楽しんだことを願っています。
質問がある場合は、質問してください。続行したい場合は質問してください。
フォームでの作業をどのように整理しますか?