- Zend 框架教程
- Zend 框架 - 主页
- Zend 框架 - 简介
- Zend 框架 - 安装
- 骨架应用
- Zend 框架 - MVC 架构
- Zend 框架 - 概念
- Zend 框架 - 服务管理器
- Zend 框架 - 事件管理器
- Zend 框架 - 模块系统
- 应用结构
- Zend 框架 - 创建模块
- Zend 框架 - 控制器
- Zend 框架 - 路由
- Zend 框架 - 视图层
- Zend 框架 - 布局
- 模型和数据库
- 不同的数据库
- 表格和验证
- Zend 框架 - 文件上传
- Zend 框架 - Ajax
- Cookie 管理
- 会话管理
- Zend 框架 - 身份验证
- 电子邮件管理
- Zend 框架 - 单元测试
- Zend 框架 - 错误处理
- Zend 框架 - 工作示例
- Zend 框架有用的资源
- Zend 框架 - 快速指南
- Zend 框架 - 有用的资源
- Zend 框架 - 讨论
Zend 框架 - 表单和验证
Zend Framework 提供了一个单独的组件zend-form来加速表单创建和验证过程。它连接模型和视图层。它提供了一组表单元素来从预定义的模型创建成熟的 html 表单,一个用于根据表单验证模型的InputFilter类以及将数据从表单绑定到模型的选项,反之亦然。
安装表单组件
Zend 表单组件可以使用Composer命令安装,如下所示 -
composer require zendframework/zend-form
Zend 表单框架具有三个子组件来管理表单。它们的详细解释如下 -
Elements - 用于定义映射到模型中属性的单个 html 输入控件。
Fieldset - 用于以嵌套方式对元素和其他字段集进行分组。
表单- 用于创建 html 表单,由元素和字段集组成。
Zend Forms 通常创建在module//src/Form目录下。
例子
现在让我们创建一个简单的表单来将书籍添加到数据库中。为此,我们应该遵循以下步骤 -
第1步:创建BookForm
在 *myapp/module/Tutorial/src/Form 目录下创建“BookForm.php”。在文件中添加以下更改 -
<?php
namespace Tutorial\Form;
use Zend\Form\Form;
class BookForm extends Form {
public function __construct($name = null) {
parent::__construct('book');
$this->add(array(
'name' => 'id',
'type' => 'Hidden',
));
$this->add(array(
'name' => 'author',
'type' => 'Text',
'options' => array(
'label' => 'Author',
),
));
$this->add(array(
'name' => 'title',
'type' => 'Text',
'options' => array(
'label' => 'Title',
),
));
$this->add(array(
'name' => 'submit',
'type' => 'Submit',
'attributes' => array(
'value' => 'Go',
'id' => 'submitbutton',
),
));
}
}
Form类提供了一个add 方法来映射模型及其相应的表单详细信息。我们通过扩展Form类创建了BookForm ,并为Book模型添加了表单详细信息。
第2步:更新书籍模型Book.php
使用过滤器和验证更新模型“Book” ,如下所示 -
<?php
namespace Tutorial\Model;
use Zend\InputFilter\InputFilterInterface;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilter;
class Book implements InputFilterAwareInterface {
public $id;
public $author;
public $title;
protected $inputFilter;
public function setInputFilter(InputFilterInterface $inputFilter) {
throw new \Exception("Not used");
}
public function getInputFilter() {
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$inputFilter->add(array(
'name' => 'id',
'required' => true,
'filters' => array(
array('name' => 'Int'),
),
));
$inputFilter->add(array(
'name' => 'author',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$inputFilter->add(array(
'name' => 'title',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'min' => 1,
'max' => 100,
),
),
),
));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
public function exchangeArray($data) {
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->author = (!empty($data['author'])) ? $data['author'] : null;
$this->title = (!empty($data['title'])) ? $data['title'] : null;
}
}
每个模型都应该实现InputFilterAwareInterface。InputFilterAwareInterface 提供了两个方法:setInputFilter()和getInputFilter()。
getInputFilter 用于获取模型的验证详细信息。Zend 框架提供了一组丰富的过滤器和验证器来验证表单。书籍模型中使用的一些过滤器和验证器如下 -
StripTags - 删除不需要的 HTML。
StringTrim - 删除不必要的空白。
StringLength 验证器- 确保用户输入的字符数不超过指定的限制。
步骤 3:更新 BookTable 类
包含saveBook方法以将书籍添加到数据库。
书表.php
<?php
namespace Tutorial\Model;
use Zend\Db\TableGateway\TableGatewayInterface;
class BookTable {
protected $tableGateway;
public function __construct(TableGatewayInterface $tableGateway) {
$this->tableGateway = $tableGateway;
}
public function fetchAll() {
$resultSet = $this->tableGateway->select();
return $resultSet;
}
public function getBook($id) {
$id = (int) $id;
$rowset = $this->tableGateway->select(array('id' => $id));
$row = $rowset->current();
if (!$row) {
throw new \Exception("Could not find row $id");
}
return $row;
}
public function saveBook(Book $book) {
$data = array (
'author' => $book->author,
'title' => $book->title,
);
$id = (int) $book->id;
if ($id == 0) {
$this->tableGateway->insert($data);
} else {
if ($this->getBook($id)) {
$this->tableGateway->update($data, array('id' => $id));
} else {
throw new \Exception('Book id does not exist');
}
}
}
}
步骤 4:更新TutorialController 类
在教程控制器中添加一个新操作 addAction – myapp/module/Tutorial/src/Controller/TutorialController.php。
public function addAction() {
$form = new BookForm();
$form->get('submit')->setValue('Add');
$request = $this->getRequest();
if ($request->isPost()) {
$book = new Book();
$form->setInputFilter($book->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$book->exchangeArray($form->getData());
$this->bookTable->saveBook($book);
// Redirect to list of Tutorial
return $this->redirect()->toRoute('tutorial');
}
}
return array('form' => $form);
}
addAction方法执行以下过程 -
获取请求对象。
检查请求的http方法是否是post方法。
如果请求的http方法不是post,它只是渲染模板add.phtml
如果请求的http方法不是post,那么它会设置inputfilter,获取请求数据并将其设置到inputfiler中。
使用Form 类的isValid()方法检查表单是否有效。
如果表单无效,它会再次呈现模板add.phtml
如果表单有效,它将将该书保存到数据库中并重定向到主页。
第5步:添加add.phtml模板
在 myapp/module/Tutorial/view/tutorial/tutorial/add.phtml 下创建模板 – add.phtml
添加.phtml
<?php
$title = 'Add new Book';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
if(!empty($form)) {
$form->setAttribute('action', $this->url('tutorial', array('action' => 'add')));
$form->prepare();
echo $this->form()->openTag($form);
echo $this->formHidden($form->get('id'));
echo $this->formRow($form->get('author'))."<br>";
echo $this->formRow($form->get('title'))."<br>";
echo $this->formSubmit($form->get('submit'));
echo $this->form()->closeTag();
}
在这里,我们使用Form实例$form渲染书籍表单。
第 6 步:运行应用程序
现在,我们可以运行应用程序 - http://localhost:8080/tutorial/add。
表格页
验证错误页面
