The 2011 Universiade in Shenzhen was coming soon, so Shenzhen University wanted to develop a volunteer service station, and I was arrested by the Youth League Committee to serve. In the process, I wrote a small form toolkit. The verification function is not implemented by myself and relies on Kohana_Validate (Kohana V3.0x branch). But I followed what "J2EE Disgusting Mode" said and got a synchronization token to prevent repeated submission of forms. This component also sets up an abstraction layer. I think the verification code can also be integrated as a token subclass. But I don’t have time to do it now, so I’ll share the code first and wait for it to be sold~
- namespace Form;
- use VolunteerFormAbstractForm;
- class NewsPoster extends AbstractForm
- {
- /**
- * Add all preset form elements
- *
- * @abstract
- */
- public function bindAllElement()
- {
- $this->bindToken(' valid_token')
- ->addElement('news title', 'title',
- array(
- 'not_empty' => null,
- 'max_length' => array(60)
- ),
- array(
- 'trim ' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('Abbreviated URL', 'urlName',
- array(
- 'max_length' => array(50 ),
- 'regex' => array('~^[a-zA-Z0-9-%]+$~')
- ),
- array(
- 'trim' => null,
- 'urlencode' = > null,
- ))
- ->addElement('keyword', 'keyWords',
- array(
- 'max_length' => array(100),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('News Author & Reporter', 'author',
- array(
- 'max_length' => array(60),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('news editor', 'editor',
- array(
- 'max_length ' => array(60),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('News source', 'source',
- array(
- 'max_length' => array(60),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('News Category', 'category',
- array(
- 'not_empty' => null,
- 'digit' => null,
- 'regex' => array('~^[1- 9]d*$~')
- ),
- array())
- ->addElement('cover image', 'cover_image',
- array(),
- array(
- 'trim' => null,
- ' urlencode' => null,
- ))
- ->addElement('Contains media information', 'mediaTag',
- array(
- 'not_empty' => null,
- 'digit' => null,
- 'regex ' => array('~^[0123]$~')
- ),
- array())
- ->addElement('News summary', 'summary',
- array(
- 'max_length' => array (255),
- ),
- array(
- 'trim' => null,
- 'htmlspecialchars' => array(ENT_QUOTES),
- ))
- ->addElement('release status', 'state',
- array(
- 'not_empty' => null,
- 'digit' => null,
- 'regex' => array('~^[01]$~')
- ),
- array())
- -> ;addElement('"Whether to display on the homepage" option', 'showInHome',
- array(
- 'boolean' => null,
- ),
- array())
- ->addElement('News content', 'content ',
- array(
- 'not_empty' => null,
- ),
- array(
- 'tidy_parse_string' => array(
- array(
- 'indent' => true,
- 'output-xhtml' => true,
- 'clean' => true,
- 'drop-font-tags'=> true,
- 'show-body-only'=> true
- ), 'UTF8'),
- 'trim' => ; null
- ));
- }
- }
Copy code
- public function action_GET()
- {
- // Get the ID in the url
- $id = $this->request->param('id', null);
-
- // Send the token to View layer (placed in the hidden field of the form)
- $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()) {
- // Verification passed, call the domain model to perform the operation~
- $this->view['success'] = true;
- } else {
- $this->view['success' ] = false;
- // Send the error message back to the view layer
- $this->view['message'] = array_values($form->getMessage());
- }
- }
Copy code
|