PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了php-在Zend Framework中处理记录表单的最佳方法大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

一旦您对教程中的示例之后建立的基本记录表单感到满意,您就会意识到您想要更专业设计的记录表单.例如.我不想在“用户”和“管理”区域中为同一张表重复记录形式.

1)是否有人使用某种机制(可能是继承)来减少几乎相似的管理员用户表单的重复?这是繁重的工作,还是有时候您最好只复制粘贴?

2)有没有人认为建立一些基本的Record类是一个好主意

>可以确定页面上的几种记录形式中,当前帖子专门针对该记录形式
>可以以某种有组织的方式区分“编辑”或“删除”按钮的单击.

3)我目前的实践包括将所有表单配置代码(装饰器,验证,初始值)放入构造函数中,并将表单提交处理放入单独的ProcessSubmit()方法中,以释放不需要代码的控制器.

以上所有内容都满足了某些预期的Record Form功能,并且我想知道是否有任何指导方针,适用于此类稍微先进的记录处理的好的示例应用程序,或者人们仍在重新虑.想知道应该走多远以及应该在哪里进行此类改进…

解决方法:

几个建议:

首先-在对表单进行子类化时,使用init()函数而不是构造函数添加元素.设置要传递给类的参数后,将执行init()函数.

其次-您无需设置表单的子类-只需设置一个“选项”即可启用管理功能

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部分:我的代码中的编辑表单是从模型的函数返回的:$record-> getEditForm()控制器代码最终看起来像这样

  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中处理记录表单的最佳方法所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: