Learn to Code a PHP Form Framework
-
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: 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. We need a base widget class (for form elements) and a base validator class for form elements and widgets. 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? Thanks before.
-
Answer:
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.
bertzzie at Stack Overflow Visit the source
Other answers
I use CakePHP or Zend_Form now on various projects, and I'd never use this myself anymore, but a few years ago I wrote a relatively complete form library like you're discussing, for a little-used framework that you've never heard of. Anyway, this is PHP4 code, but it was used in production and worked well for us. In this case, the form class itself is Abstract, you inherit it, and define your form. The InputField class you use to help you create your forms: http://pastebin.com/EP8ngv8E The Webform class: http://pastebin.com/4kgsFFsZ I don't advocate you actually try to use any of this. For one, you'd have to remove or emulate all the other dependencies it has (and it has many, foremost from the looks of it, the Error and Warning classes the framework used for error handling). But I do think that if you're interested in the subject, it might be interesting to you.
Encoderer
I have created a form library for Code igniter framework .. actually a joomla component xCIDeveloper which lets you develop joomla components in CI framework. this Component have a nice form library with a lot of features. you can check it by downloading from http://www.xavoc.com => download section. Yes you must install it in joomla and search for form.php file
Gowrav Vishwakarma
Related Q & A:
- How do I create a Cocoa Touch Framework?Best solution by stackoverflow.com
- Are SQL Injection vulnerabilities in a PHP application acceptable if mod_security is enabled?Best solution by Programmers
- How to dynamically create a PHP class name on the fly?Best solution by stackoverflow.com
- How to make a php article counter?Best solution by Stack Overflow
- How to start creating a php script, that will be installed on many servers?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.