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 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了