一旦您对根据教程中的示例构建的基本记录表单感到满意,您就会意识到您需要设计更专业的记录表单。例如。我不想在用户和管理区域中为同一个表复制记录表单。
1) 是否有人使用某种机制(可能是继承)来减少几乎相似的管理员和用户表单的重复?这样做是不是很麻烦,或者有时你最好只复制粘贴?
2) 有没有人认为构建一些基本的 Record 类是个好主意
3) 我目前的做法包括将所有表单配置代码(装饰器、验证、初始值)放入构造函数中,并将表单提交处理放入单独的 ProcessSubmit() 方法中以释放控制器不必要的代码。
以上所有地址都指向了一些预期的记录表单功能,我想知道是否有任何指导方针、用于这种稍微高级的记录处理的优秀示例应用程序,或者人们仍在重新设计轮子。想知道你应该走多远以及应该在哪里停下来......
几个建议:
首先 - 当你对表单进行子类化时,使用
第二个——不用子类化你的表单——你可以设置一个"选项"来启用管理功能: (短码网-duanma.net)
class My_Record_Form extends Zend_Form {
protected $_record = null;
public function setRecord($record) {
$this-_record = $record;
}
public function getRecord() {
if ($this-_record === null || (!$this-_record instanceOf My_Record)) {
throw new Exception("Record not set - or not the right type");
}
return $this-_record;
}
protected $_admin = false;
public function setAdmin($admin) {
$this-_admin = $admin;
}
public function getAdmin() { return $this-_admin; }
public function init() {
$record = $this-getRecord();
$this-addElement(......);
$this-addElement(......);
$this-addElement(......);
if ($this-getAdmin()) {
$this-addElement(.....);
}
$this-setDefaults($record-toArray());
}
public function process(array $data) {
if ($this-isValid($data)) {
$record = $this-getRecord();
if (isset($this-delete) && $this-delete-getValue()) {
// delete button was clicked
$record-delete();
return true;
}
$record-setFromArray($this-getValues());
$record-save();
return true;
}
}
}
然后在您的控制器中,您可以执行以下操作:
$form = new My_Record_Form(array(
'record'=$record,
'admin'=My_Auth::getInstance()-hasPermission($record, 'admin')
));
制作一个处理管理事务的 My_Record_Admin_Form 并没有什么"错误"——但我发现这种方法将所有"记录表单"代码保存在一个地方,并且更容易维护。
回答第 2 部分:我的代码中的编辑表单是从模型的函数返回的:
protected $_domain = null;
protected function _getDomain($allowNew = false)
{
if ($this-_domain)
{
return $this-view-domain = $this-_domain;
} else {
$id = $this-_request-getParam('id');
if (($id == 'new' || $id=='') && $allowNew)
{
MW_Auth::getInstance()-requirePrivilege($this-_table, 'create');
$domain = $this-_table-createRow();
} else {
$domain = $this-_table-find($id)-current();
if (!$domain) throw new MW_Controller_404Exception('Domain not found');
}
return $this-view-domain = $this-_domain = $domain;
}
}
public function editAction()
{
$domain = $this-_getDomain(true);
MW_Auth::getInstance()-requirePrivilege($domain,'edit');
$form = $domain-getEditForm();
if ($this-_request-isPost() && $form-process($this-_request-getPost()))
{
if ($form-delete && $form-delete-getValue())
{
return $this-_redirect($this-view-url(array(
'controller'='domain',
'action'='index',
), null, true));
} else {
return $this-_redirect($this-view-url(array(
'controller'='domain',
'action'='view',
'id'=$form-getDomain()-id,
), null, true));
}
}
$this-view-form = $form;
}
所以——例如,记录的实际 id 在 URI /domain/edit/id/10 中传递。如果您要将多个这些表单放在一个页面上 - 您应该确保将表单的"action"属性设置为指向特定于该表单的操作。
我创建了一个
但是,说实话,我真的不喜欢使用
以上就是短码网小编为大家整理的《php Zend Framework中处理记录表单的最佳方式》相关内容,希望大家喜欢。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若内容造成侵权/违法违规/事实不符,请将联系本站反馈,一经查实,立即处理!
《php Zend Framework中处理记录表单的最佳方式》文档下载仅供参考学习,下载后请在24小时内删除。
转载注明出处:https://www.duanma.net/article/386b1461941.html