-
Notifications
You must be signed in to change notification settings - Fork 26
ObjectForm
The form helper provided with CodeIgniter is a good idea to help view creators in managing forms ; for example, the form_dropdown() function is really time and code length improving, but some function prototypes don't suit me. I hope this new helper will be useful to you as it was for me. I first created it with PHP5 notations, but I revised my jugement to make it PHP4 compatible and share it.
You'll find to complete and commented code at the end of this document. Name it as you want (I've choosen objform_helper.php).
I've reuse the form helper documentation for most of methods description, as they're just equivalents.
[h3]Object oriented[/h3] The first big difference is that I've coded it as a class instead of a serie of function, because I prefer the object syntax even a views. So once you've loaded the helper, you've to instanciate a [b]Form[/b] object: [code] $this->load->helper('objform'); $viewdata['form'] = new Form(); ... [/code] Then you can use the public methods, which are for most of them equivalents to the original form helper.
[h3]Methods list[/h3]
[h4]open()[/h4] [code]open($action, $post = true, $name = '', $id = false, $attributes = null, $append = true)[/code] Creates an opening form tag with a base URL [b]built from your config preferences[/b]. It will optionally let you choose between the get and post methods, and add attributes. The last three parameters are common to all element definition methods, see [b]The 3 magic parameters[/b] at the end for an explaination of their use.
[h4]open_multipart()[/h4] [code]open_multipart($action, $name = '', $id = false, $attributes = null, $append = true)[/code] This function is absolutely identical to the open() method above except that it adds a multipart attribute, which is necessary if you would like to use the form to upload files with. The post method is also automatically set.
[h4]close()[/h4] [code]close($append = false)[/code] Produces a closing </form> tag. The only advantage to using this function is it permits you use the last of the 3 common parameters, and being consistent with the helper usage.
[h4]hidden()[/h4] [code]hidden($name, $value = '', $id = false, $attributes = null, $append = true)[/code] Lets you generate hidden input fields.
[h4]text()[/h4] [code]text($name = '', $value = '', $id = false, $attributes = null, $append = true)[/code] Lets you generate a standard text input field.
[h4]password()[/h4] [code]password($name = '', $value = '', $id = false, $attributes = null, $append = true)[/code] This function is identical in all respects to the text() method above except that is sets it as a "password" type.
[h4]upload()[/h4] [code]upload($name = '', $value = '', $id = false, $attributes = null, $append = true)[/code] This function is identical in all respects to the text() method above except that is sets it as a "file" type, allowing it to be used to upload files.