Creating and Saving Forms

Creating forms can often be a very time consuming process but fortunately PHP Wax tries to make the process a lot easier.

To make things easier to understand we're going to cover two different types of form in this tutorial, forms that are bound to models, and forms that are standalone.

Handling Bound Forms

Since you've done all the hard work in your model definition it makes sense to reuse that application logic to create your forms, since it's often the case that forms are just a way of getting data from the user into the database. We'll use the User model we created in the earlier tutorial to demonstrate how this works.

Remembering the correct way to separate our application code from the templates we'll create a form in our controller and then create a template to render it.

Here's the controller code which you can drop into app/controller/PageController.php:

  public function test() {
    $user = new User;
    $fred = $user->filter("username", "fred")->first();
    $this->form = new WaxForm($fred);
  }

Then we need a template to render our form. For this example we'll create an html page in app/view/page/test.html. All we need is the following code:

  <h1>Form for Fred</h1>
  <?=$form->render();?>

Ok, now that does a little too scary that it can be that easy but in reality there's plenty of cases when you want to customise things to a much finer level. The good news is that PHP Wax lets you do this if you need to whilst giving you a very usable out of the box behaviour.

This kind of setup is ideal for putting together quick scaffolds and to prove it we're going to use our form to edit Fred's details. All we need to do is save the form and the data will be passed back to the User model and saved to the database. To do this we'll edit our controller code to the following:

public function test() {
  $user = new User;
  $fred = $user->filter("username", "fred")->first();
  $this->form = new WaxForm($fred);
  if($this->form->is_posted()) $this->form->save();
}

Go to http://testapp.local/test and you'll see the result of all your hard work. Now change some of Fred's details and hit the submit button. You'll see that our web application is working nicely.

Handling Unbound Forms

When you're creating forms, you don't always want to save the results to the database and for this you can use unbound or standalone forms.