dongtu0363 2011-05-30 07:44
浏览 28
已采纳

学习编写PHP表单框架

Have you ever seen a book, article, or tutorial specifically about PHP form framework? I'm not talking about a whole framework, but just the bits to handle form. For now, I've used Zend's form and Symfony's form, and I'm learning about how to build a framework, and stuck in the "how to build a form framework" part.

I've tried to read Zend's and Symfony's code, but I think it's just too big and complex, without any explanation. Do you have any recommendation, or even maybe someone mind explaining how a form framework works (or even better, how to build a form framework)?

What I've got so far:

  1. I need to create an abstract form object, so all other forms in my application can be inherited from here. The form has must have at least methods for configuring and saving.
  2. We need a base widget class (for form elements) and a base validator class for form elements and widgets.
  3. We need to somehow connects all of the forms. This is the part that confuses me. How can I connect all that elements?

Could someone please give me a hint?

  • 写回答

2条回答 默认 最新

  • dsa5211314 2011-05-30 10:16
    关注

    I have created something to abstract the creations of forms in my small framework.

    Basically what I have are two main classes. The element class and the element_container class.

    Most of my elements extend the element class except for the form, fieldset, div, etc, (elements that contain other elements) which extend the element_container class.

    This is my simple input class:

    class acs_form_input extends acs_element {
    
        public function __construct($name) {
    
            //These propertie is declared in the parent class
    
            //By default the type is set to text
            $this->setAttribute('type','text');
            $this->setAttribute('name',$name);
            $this->setAttribute('id',$name);
    
            $this->tpl_path = 'html/forms/form_input';
        }
    }
    

    Since it extends the element class I just have to say what type this is and what template it will use, the process of rendering is all done in the parent class.

    The element_container class is basically the same except it can hold other elements and so has a some extra processing when it's renderes (basically a loop rendering the html of the elements)

    I keep this is a model so here is a simple form code:

     $this->form->addText('name','Nome*');
            $this->form->addText('empresa','Empresa');
            $this->form->addText('morada','Morada')->setAttribute('size','60');
            $this->form->addText('cpostal1','C. Postal');
            $this->form->addText('cpostal2','-')->setAttribute('size','6');
            $this->form->addText('loc','Localidade');
            $this->form->addText('tel','Telefone');
            $this->form->addText('fax','Fax');
            $this->form->addText('email','E-mail*');        
            $this->form->addTextArea('msg','Mensagem*');
            $this->form->addSubmit('sub', 'Enviar »');
    

    Hope this gives you some idea of what to do to create your own form framework.

    NOTE: I can't chain the calls to form because every 'add[element]' method returns the instance of the created element.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)