`
webcode
  • 浏览: 5928601 次
  • 性别: Icon_minigender_1
  • 来自: 上海
文章分类
社区版块
存档分类
最新评论

Zend Framework 教程 – 添加新专辑

 
阅读更多

添加新专辑的功能有两部分工作要做:

  • 为用户显示一个填写资料的表单;
  • 处理表单提交和保存到数据库。

我们使用Zend_Form 来完成这任务,Zend_Form 组件允许我们创建表单和验证表单输入。我们先创建一个扩展Zend_Form 的新类Form_Album 来定义新的表单。因为这是一个应用资源,Form_Album 保存在application/forms 目录的Album.php 文件。使用zf 命令行工具创建相关文件:zf create form Album

创建的Album.php 文件默认包含了init() 方法,创建表单和添加元素。编辑application/forms/Album.php文件,移除init() 方法中的注释,并添加以下代码:

zf-tutorial/application/forms/Album.php
<?php
class Application_Form_Album extends Zend_Form
{
public function init()
{
$this->setName('album');
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');
$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');
$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');
$this->addElements(array($id, $artist, $title, $submit));
}
}

在Application_Form_Album 的init() 方法里面,我们为id, artist,title, 和 submit按钮创建了4个表单元素。对于每一项都设置了不同的属性,包括要显示的标签。对于id,Int 过滤器可以确保它只是一个整数以避免潜在的SQL注入问题。

对于text文本元素,我们添加了两个过滤器,StripTags 和 StringTrim 移除不想要的HTML和空白字符。同时设置它为必填项,通过添加NotEmpty 验证器确保用户确实输入了要求的信息(从技术上说NotEmpty验证器不是必须的,因为系统设置了setRequired() 为True,会自动添加;这里只是演示下如何使用验证)。

现在我们需要让这个表单显示并处理提交的数据。这由IndexController 控制器的 addAction() 方法完成:

zf-tutorial/application/controllers/IndexController.php
...
function addAction()
{
$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);
$this->_helper->redirector('index');
} else {
$form->populate($formData);
}
}
}
...

一起来解析下上面的代码:

$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;

实例化Form_Album类,设置submit按钮的label为“Add”,然后赋给view 去呈现。

if ($this->getRequest()->isPost()) {
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {

如果请求的isPost() 为True,即表单已经提交,我们就使用getPost() 从请求中获取表单数据,并使用isValid() 成员函数验证数据。

$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);

如果表单有效,model 类Application_Model_DbTable_Albums 进行实例化,并使用前面models层定义的addAlbum() 方法在数据库中创建新记录。

$this->_helper->redirector('index');

保存了新的专辑行数据后,使用Redirector 这个action helper 返回到 index action页面。

} else {
$form->populate($formData);
}

如果表单数据不合法,表单会重新显示并填入用户输入的数据。

然后我们要在view 脚本add.phtml中呈现表单:

zf-tutorial/application/views/scripts/index/add.phtml
<?php
$this->title = "Add new album";
$this->headTitle($this->title);
echo $this->form ;
?>

很简单,只要echo 输出$this->form 即可,表单它知道如何显示自己。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics