dqb14659 2011-06-09 16:11
浏览 57
已采纳

Zend框架表单,子表单和装饰器

I currently have a zend framework application with multiple modules. Each module should be using the same Zend_Form_Decorator_ViewScript, located in the default modules /views/scripts folder.

Without any changes, modules by default only look for form decorator viewscripts in their own /views/scripts folder located under each module, so to get them to load them from default modules folder, I first need to apply this within the form:

$view = new Zend_View();
$view->setScriptPath(APPLICATION_PATH . '/views/scripts');
$this->setView($view);

Within that same form, I create multiple Zend_Form_SubForms, for which I need to apply that same code again. If that isn't enough, I need to apply that path to each individual element in each SubForm as well as the parent form. Additionally, each element has to have its ViewScript defined each time like:

$username->setDecorators(array(array('ViewScript', array('viewScript' => '/formScripts/wizardElement.phtml'))));

Now, it all works if I define all of that for each element/subform/form within the same file, but it just seems so much unnecessary work/code.

  • Can the process be simplified firstly by just having the parent form define the scriptPath for itself, its elements, its subforms, and the subforms elements?
  • Can new elements created automatically have specific ViewScripts defined for them, based on what type of element it is (i.e. input box, checkbox, selectbox, textarea, button etc)?

I am currently extending my form directly from the default Zend_Form, I won't have a problem of creating an own abstract form to extend my forms from, but especially with the scriptPath problems, I am not entirely sure how I should approach this whole problem.

Applying:

$this->setSubFormDecorators(array(
            'Form',
            array('ViewScript', array('viewScript' => '/formScripts/wizardSubForm.phtml'))            
));

overwrites all the element specific decorators I've applied before it.

Suggestions?

  • 写回答

1条回答 默认 最新

  • doulin4844 2011-06-16 17:18
    关注

    May be I didn't get your case in details but I would suggest you create base form, then base form classes for each module then your specific forms extend the respective module form

    My_Base_Form extends Zend_Form
    {
       public function init()
       {
         //if you need to init something for all forms
         parent::init();
       }
    
       public function _createSelect($name)
       {
          $element=new Zend_Form_Element_Select($name);
          $element->setDecorators(
                 //decorators for select
             )
          $element->viewScript='select.phtml';
          return $element;
       }
    }
    
    My_Default_Form extends My_Base_Form
    {
       public function init()
       {
    
         //what you do to init dirs for this module
         $view = new Zend_View();
         $view->setScriptPath(APPLICATION_PATH . '/views/scripts');
         $this->setView($view);
         parent::init();
       }
    
       //called automatically by Zend_Form
       public function loadDefaultDecorators()
       {
    
           parent::loadDefaultDecorators();
           $this->setDefaultFormDecorators($this);
           $this->setButtonDecorators($this);
       }
    
    }
    My_Admin_Form extends My_Base_From{}
    

    To not repeat setting element decorators you may create helper methods that do that for you and put it in the base form class or in the module form class

    Default_Form_Register extends My_Default_Form
    {
         public function init()
         {
             $el=$this->_createSelect($name);
             $el->setLabel('Select');
             $this->addElement($el);
             parent::init();
         }
    }
    

    you may need to use the same approach for subforms, then put the base classes in your library and you should be ok.

    You are will be free to make common changes based on module or element type.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化