A free and open-source book on ZF3 for beginners


7.9. Example: Creating the Contact Form Model

Now that we know how to set the form name, action, and method attributes and how to add fields (elements) to the form, let's create the complete model class for the feedback form that we used in our previous examples.

As we know, form model classes for the Application module live inside the Application\Form namespace. So, we have to create the ContactForm.php file inside of the Form directory under the Application module's source directory (figure 7.14).

Figure 7.14. Form directory Figure 7.14. Form directory

We will have two methods in our form class:

We put the field creation logic into the addElements() private method to better structure the form model's code.

The code of the ContactForm class is presented below:

<?php
namespace Application\Form;

use Zend\Form\Form;

/**
 * This form is used to collect user feedback data like user E-mail, 
 * message subject and text.
 */
class ContactForm extends Form
{
  // Constructor.   
  public function __construct()
  {
    // Define form name
    parent::__construct('contact-form');

    // Set POST method for this form
    $this->setAttribute('method', 'post');
        	
    // Add form elements
    $this->addElements();    
  }
    
  // This method adds elements to form (input fields and 
  // submit button).
  private function addElements() 
  {
    // Add "email" field
    $this->add([
	        'type'  => 'text',
            'name' => 'email',
            'attributes' => [                
                'id' => 'email'
            ],
            'options' => [
                'label' => 'Your E-mail',
            ],
        ]);
        
    // Add "subject" field
    $this->add([
            'type'  => 'text',
            'name' => 'subject',
            'attributes' => [
              'id' => 'subject'  
            ],
            'options' => [
                'label' => 'Subject',
            ],
        ]);
        
    // Add "body" field
    $this->add([
            'type'  => 'text',
            'name' => 'body',			
            'attributes' => [                
			  'id' => 'body'
            ],
            'options' => [
                'label' => 'Message Body',
            ],
        ]);
        
    // Add the submit button
    $this->add([
            'type'  => 'submit',
            'name' => 'submit',
            'attributes' => [                
                'value' => 'Submit',
            ],
        ]);
    }        
}

In line 10 above, we define the ContactForm class which extends the Form base class.

In lines 13-23, we have the constructor method. It calls the base class' constructor (line 16) and passes the form name as its argument ("contact-form"). In line 19, the base class' setAttribute() method is called allowing you to set the method name for the form (we set the POST method).

In line 22, the addElements() private method is called, which does the actual work of adding elements to the form. The code of the addElements() is located in lines 27-73. To add elements to the form, we call the add() method provided by the base class. This method accepts the single argument -- an array containing configuration for an element. We add four fields: the email, the subject, the body and the submit field.

In figure 7.15, you can see a schematic graphical representation of the form model we have created.

Figure 7.15. The feedback form model and its elements Figure 7.15. The feedback form model and its elements


Top