Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
  1. Code
  2. PHP

Easy Form Generation Using FuelPHP

Read Time: 10 mins

Thanks to FuelPHP's fieldset class, working with forms couldn't be easier. With a few lines of code, you can easily generate and validate a form. Today, we're going to learn how to do just that!

The Fieldset class is used to create a form and handle its validation in an object-oriented way. It uses the Form and Validation classes. This class, itself, is only meant to model the fieldset and its fields, while the other two classes perform the brunt of the work.

Setup Fuel

We need a FuelPHP installation with an RM package enabled. I'm going to use a MySQL database with a sample table. While the Fieldset class can be configured to use a normal model, using an ORM will save us some time.

If you haven't reviewed the first couple parts of the FuelPHP series here on Nettuts+, now is a great time to check out part one and two, by Phil Sturgeon.

Set up a database connection at fuel/app/config/development/db.php.

Enable the ORM package through fuel/app/config.php

And, finally, here's the SQL for the table I'm using for this tutorial.


We need a model for our controller to interact with the posts table. Go ahead and create a post.php inside app/classes/model/. Create a Model_Post class and make sure it extends \Orm\Model. The ORM will automatically use the posts table in our database since we have used the singular of "posts". If you want to set a different table, set up a static property called $_table_name.

Setting Up the Properties

We should specify the columns of our posts table within our model. At the same time, we can also set up labels, form validation rules to use with our fieldset class to generate the form. All of these go in an associated array, called $_properies. With everything in place, our final model should look like so:

Let's examine what options we can use. data_type simply holds the fields's type. It could be either string, integer or mysql_date. The value for the label property will be shown as the field label once the form is generated. validation accepts an array of validation rules. By default, these fields will be text input fields. Using the form, you can make it a select or texarea.

The ORM treats the column named id as the primary and will not be shown when generating a form. If your table's primary key column is different, use the $_primary_key property to specify it.


Now that the model is ready, let's create the controller. Controllers should be placed within fuel/app/classes/controller/. I've created a controller, called Controller_Posts (posts.php) and extended it from Controller_Template.

Users will be able to see a list of posts, add new ones, or edit an existing one. Because I'm using the template controller, I can use a base template file to work with. Templates go in fuel/app/views/template.php

This is merely standard HTML markup with the Twitter bootstrap. The $content variable will have the content. We can set an array of messages and if we do, it will be printed as an unordered list.

Adding New Posts

This is where the fun begins. We're going to generate the form for adding new posts. As you might have guessed, we'll be working with the action_add() method. Let's generate the form and pass it to our template.

Fieldset::forge() will return a new instance of the fieldset class. It's the same as doing new Fieldset. However, using the forge method here, we can name our instances. If we call an instance twice with the same name, an existing instance will be returned if available [the Factory pattern]. To name your instance, pass the name to the forge method. Fieldset::forge('new_post')

Using the add_model method, we pass the model which we want the forms to be generated from. Fieldset will grab the data from $_properties to generate the form. Calling the form() method from the fieldset object will return an instance from Form class, and by calling the build() method, we can get a html (string) output of the form.

Finally, we pass the $form to the template as content. Another method of passing variables to a template is $this->template->content = $form.

Fire up your browser and navigate to http://path_to_site/index.php/posts/add. You should see a form identical to this.

No submit button? Let's fix that. We need to add a new field to our form object.

Using the add method we can add additional fields to our form. First parameter is our new fields name, second is for label, for the third parameter we pass an array of attributes.

After adding this, our action_add() will look like this.

And our form..

Validation and Saving

Now that we have a nice form, let's validate it and save to the database. The fieldset object includes an instance from FuelPHP's validation class. All the rules has been applied and ready to go.

$fieldset->validation() returns a validation class instance and by accessing its run() method we can check if validation is passed. If so, we add a new post to our database. $fieldset->validated() will return an array of validated fields. If validation is passed and post is saved, the user will be redirected to the edit page, otherwise pass the validation errors to our template as message variable.

If you try to submit some invalid data, you will get an output like so:

Everything seems fine except for one issue: data we submit doesn't appear after the page refresh. Not to worry, one method call and you're done.

Cool, huh? Add some valid data and it will redirect to the action_edit() method, which is not ready yet.

Editing a Post

Editing a section is pretty much the same as our add post section. Except we need to populate the data with an existing post. I'm going to duplicate the action_add code.

With some small modifications to our action_add() method, we have our edit method. repopulate() method has been replaced by populate() method. Using the populate method, we can populate a form with an existing post's data.

In this case, we grab the post from our database using the $id parameter, then pass it to the requisite method. We don't need $post = new Model_Post; anymore because we are not adding any thing to the database. The $post object we create in the beginning is used to assign the new values. Once edited it will redirect back to its edit screen. We're done! Add some posts, and try editing them.

Listing Pages

Let's build up the listing section so users can see all the posts in one place.

The listing is handled by the action_index() method

Model_Post::find('all') will return an array of posts objects for all of our posts. Using View::forge(), a new view object is instantiated. The parameter for View::forge() is the name for our specific view. It's located at app/views/listing.php. The array of posts object ($posts) is then passed to our view. The Listing view will take care of the listing and finally we assign the view to $this->template->content.

In the view, we loop through $posts and generate the list.

If you have any posts in the database, it will look something like this.

Some Final Modifications

Everything seems to be working correctly; however, there are some minor issues. The generated form has a text field for the post content, which would be better off as a textarea.

You can pass all the field types text, textarea, select, radio etc. For select or radio elements, you can set the options. Setting options for a select using another table is also possible. If you want to change the default layout, place a form config file in fuel/app/config/form.php If you're not sure about what to put in there, copy stuff from fuel/core/config/form.php. Fuel uses this file to generate the forms.


I hope you now have a clear understanding of the fieldset class. If you have any questions, please let me know in the comments below. Thank you so much for reading!

Did you find this post useful?
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.