I'm currently playing around with HTML_QuickForm for generating forms in PHP. It seems kind of limited in that it's hard to insert my own javascript or customizing the display and grouping of certain elements.
Are there any alternatives to QuickForm that might provide more flexibility?
If you find it hard to insert Javascript into the form elements, consider using a JavaScript framework such as Prototype or jQuery. There, you can centralize the task of injecting event handling into form controls.
By that, I mean that you won't need to insert event handlers into the HTML form code. Instead, you register those events from somewhere else. For example, in Prototype you would be able to write something like this:
$('myFormControl').observe('click', myClickFunction)
Also have a look at the answers to another question.
/EDIT: of course, you can also insert custom attributes and thus event handlers into the form elements using HTML_QuickForm. However, the above way is superior.
I've found the Zend_Form package of the Zend Framework to be particulary flexible. This component can also be used with Zend_Dojo to rapidly implement common javascript form helpers. However, the component is agnostic when it comes to the library that you use, but supports Dojo naitively. The component also allows for grouping, multi-page forms, custom decorators, validators and other features making it very flexible.
I wrote an article about this for OnLamp: Autofilled PHP Forms
My beef with HTML_QuickForm and Zend_Form and all the other form-handling frameworks I could find is that they seem to assume that you'll be writing code to generate the form. But that didn't match my development process, where I'd start with the LOOK of the page (specified via HTML templates) and add functionality to it.
In my view of the world, form handling boils down to:
fillInFormValues() makes 2, 3, and 5 really easy.
I'll second Zend_Form; it has an excellent ini style implementation that allows you to define a form extremely quickly:
[main]
vessel.form.method = "post"
vessel.form.elements.name.type = "text"
vessel.form.elements.name.name = "name"
vessel.form.elements.name.options.label = "Name: "
vessel.form.elements.name.options.required = true
vessel.form.elements.identifier_type.type = "select"
vessel.form.elements.identifier_type.name = "identifier_type"
vessel.form.elements.identifier_type.options.label = "Identifier type: "
vessel.form.elements.identifier_type.options.required = true
vessel.form.elements.identifier_type.options.multioptions.IMO Number = "IMO Number";
vessel.form.elements.identifier_type.options.multioptions.Registry organisation and Number = "Registry organisation and Number";
vessel.form.elements.identifier_type.options.multioptions.SSR Number = "SSR Number";
vessel.form.elements.identifier.type = "text"
vessel.form.elements.identifier.name = "identifier"
vessel.form.elements.identifier.options.label = "Identifier: "
vessel.form.elements.identifier.options.required = true
vessel.form.elements.identifier.options.filters.lower.filter = "StringToUpper"
vessel.form.elements.email.type = "text"
vessel.form.elements.email.name = "email"
vessel.form.elements.email.options.label = "Email: "
vessel.form.elements.email.options.required = true
vessel.form.elements.owner_id.type = "hidden"
vessel.form.elements.owner_id.name = "owner_id"
vessel.form.elements.owner_id.options.required = true
; submit button
vessel.form.elements.submit.type = "submit"
vessel.form.elements.submit.name = "Update"
vessel.form.elements.submit.option.value = "Update"
With Zend_Form it is entirely possible to start with your form visually, and then work backwords.
This is done by removing all decorators and replacing them with a ViewScript decorator
$this->form->setDecorators( array(array('ViewScript', array('viewScript' => 'forms/aform.phtml'))));
And in that viewscript you would do something like this:
<?=$this->element->title->renderViewHelper()?>
Going with this approach, you can basically do anything you want with the form.
Another great thing about Zend_Form is that you can create custom elements which can encapsulate other stuff in them. For example, you can have an element which outputs a textarea and then some Javascript to turn it into a WYSIWYG area.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With