今天我们来讲解一下zendframework之zend_form初步使用
下面是一个zend_form添加类别之demo:
可以添加验证规则,错误提示,增加form修饰,
还有结合数据处理等,请结合手册加以学习
创建forms目录与models目录同一级
Category.php:
复制代码
action里分页面action及数据处理action, 可在一个action处理
复制代码
index.phtml
<?php echo $this->form; ?>
Model处理添加
复制代码
下面是一个zend_form添加类别之demo:
可以添加验证规则,错误提示,增加form修饰,
还有结合数据处理等,请结合手册加以学习
创建forms目录与models目录同一级
Category.php:
- <?php
- class Form_Category extends Zend_Form
- {
- public function init(){
- $this->setMethod('post');
- $this->setAction('/zf/public/index/add');
- $category_id = $this->createElement('hidden', 'id');
- $this->addElement($category_id);
- $title = $this->createElement('text','name');
- $title->setLabel('类名');
- $title->setRequired(TRUE);
- $title->setAttrib('size', '20');
- $title->addErrorMessage('类名不能为空');
- $title->addValidator('stringLength', false, array(3, 96));
- $this->addElement($title);
- $this->addElement('submit','sumbit',array('label'=>'添加'));
- }
- }
action里分页面action及数据处理action, 可在一个action处理
- /**
- * index action test
- *
- */
- public function indexAction()
- {
- //modelUser
- $modelUser = new Model_User();
- $result = $modelUser->getUserById(1);
- //formCategory
- $form = new Form_Category();
- $request = $this->getRequest();
- if ($this->getRequest()->isPost())
- {
- if ($form->isValid($request->getPost()))
- {
- $modelCategory = new Model_Category();
- $modelCategory->add($form->getValue('name'));
- }
- }
- //view
- $this->view->result = $result;
- $this->view->form = $form;
- }
index.phtml
<?php echo $this->form; ?>
Model处理添加
- <?php
- class Model_Category extends Zend_Db_Table_Abstract
- {
- protected $_name = 'category';
- protected $_primary = 'id';
- /**
- * 添加类别名字
- *
- * @param string $name
- * @return integer
- */
- public function add($name)
- {
- $row = $this->createRow();
- $row->name = $name;
- return $row->save();
- }
- }
