学习编写PHP表单框架

ber*_*zie 6 php forms frameworks

您是否曾经看过有关PHP表单框架的书籍,文章或教程?我不是在讨论整个框架,而只是处理表单的位.现在,我已经使用了Zend的形式和Symfony的形式,我正在学习如何构建框架,并坚持" 如何构建表单框架 "部分.

我试过阅读Zend和Symfony的代码,但我认为它太大而复杂,没有任何解释.您是否有任何建议,甚至可能有人介意解释表单框架的工作原理(甚至更好,如何构建表单框架)?

到目前为止我得到了什么:

  1. 我需要创建一个抽象的表单对象,因此我的应用程序中的所有其他表单都可以从这里继承.表单必须至少具有配置和保存的方法.
  2. 我们需要一个基本窗口小部件类(用于表单元素)和一个用于表单元素和窗口小部件的基本验证器类.
  3. 我们需要以某种方式连接所有表格.这是令我困惑的部分.我如何连接所有元素?

有人可以给我一个提示吗?

Ant*_*oCS 0

我在我的小框架中创建了一些东西来抽象形式的创建。

\n\n

基本上我有两个主要课程。元素类和 element_container 类。

\n\n

我的大多数元素都扩展了元素类,除了 form、fieldset、div 等(包含其他元素的元素)扩展了 element_container 类。

\n\n

这是我的简单输入类:

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

因为它扩展了元素类,所以我只需要说明它是什么类型以及它将使用什么模板,渲染的过程都是在父类中完成的。

\n\n

element_container 类基本上是相同的,只是它可以容纳其他元素,因此在渲染时有一些额外的处理(基本上是一个循环渲染元素的 html)

\n\n

我保留这是一个模型,所以这里是一个简单的表单代码:

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

希望这能让您了解如何创建自己的表单框架。

\n\n

注意:我无法链接对 form 的调用,因为每个“add[element]”方法都会返回创建的元素的实例。

\n