話說深圳 2011 的大運會不是要來啦嘛,於是深大要開發一個志工服務站,我被團委抓去服役了。過程中寫了一個小小的表單工具包。驗證功能不是我自己實現的,依賴 Kohana_Validate(Kohana V3.0x 分支)。但自己就按照 《J2EE 噁心模式》所說,弄了個同步令牌,防止重複提交表單。這個元件也設定了抽象層,我覺得可以把驗證碼也當作一個令牌子類別整合。但現在沒時間弄,先把程式碼分享上來等被拍磚啦~
-
namespace Form;
- use VolunteerFormAbstractForm;
-
- class NewsPoster extends Abstract; */
- public function bindAllElement()
- {
- $this->bindToken('valid_token')
- ->addElement('新聞標題', 'title',
- array(
- 'not_empty' => null,
- 'max_length' => array(60)
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array( ENT_QUOTES),
- ))
- ->addElement('縮寫網址', 'urlName',
- array(
- 'max_length' => array(50),
- 'regex' = > array('~^[a-zA-Z0-9-%]+$~')
- ),
- array(
- 'trim' => null,
- 'urlencode' => null,
- ))
- ->addElement('關鍵字', 'keyWords',
- array(
- 'max_length' => array(100),
- ),
- array (
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('新聞作者&記者', 'author',
- array(
- 'max_length' => array(60),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- 'htmlspecialchars' => array(ENT_QUOTES),
- 'htmlspecialchars' => array(ENT_QUOTES),
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('新聞編輯', 'editor',
- array(
- 'max_length' => array(60),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('新聞來源', 'source',
- array(
- ' max_length' => array(60),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- 'htmlspecialchars' => array(ENT_QUOTES),
- html)
- ))
- ->addElement('新聞分類', 'category',
- array(
- 'not_empty' => null,
- 'digit' => null,
- 'regex' => array('~ ^[1-9]d*$~')
- ),
- array())
- ->addElement('封面圖片', 'cover_image',
- array(),
- array(
- 'trim' => null,
- 'urlencode' => null,
- ))
- ->addElement('包含媒體資訊', 'mediaTag',
- array(
- 'not_empty' => null,
- 'digit' => null,
- 'regex' => array('~^[0123]$~')
- ),
- array() )
- ->addElement('新聞摘要', 'summary',
- array(
- 'max_length' => array(255),
- ),
- array(
- 'trim ' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('發佈狀態', 'state',
- array(
- 'not_empty' => null,
- 'digit' => null,
- 'regex' => array('~^[01]$~')
- ),
- array())
- - >addElement('“是否顯示在首頁”選項', 'showInHome',
- array(
- 'boolean' => null,
- ),
- array())
- ->addElement ('新聞內容', 'content',
- array(
- 'not_empty' => null,
- ),
- array(
- 'tidy_parse_string' => array(
- array(> array(> array(> array(> array(> array(>)
- 'indent' => true,
- 'output-xhtml' => true,
- 'clean' => true,
- 'drop-font-tags'=> true,
- ' show-body-only'=> true
- ), 'UTF8'),
- 'trim' => null
- ));
- }
- }
-
複製程式碼
- public function action_GET()
- {
- // 取得url 中的ID
- $id = $this->request->param('id', null);
-
- // 將令牌送到視圖層(放在表單的隱藏域)
- $this->view['token'] = FormNewsPoster::getToken()->useToken( );
- }
-
- public function action_POST()
- {
- $id = $this->request->param('id', null);
-
- $ form = FormNewsPoster::factory($_POST);
- $form->bindAllElement();
-
- if ($form->checkForm()) {
- // 驗證通過,呼叫領域模型執行操作~
- $this->view['success'] = true;
- } else {
- $this->view['success'] = false;
- // 將錯誤訊息送回視圖層
- $this->view['message'] = array_values($form->getMessage());
- }
- }
複製代碼
|