Create a Form in Zend Framework 3

To create a form in Zend Framework 3 requires a class to be created that extends the Zend\Form\Form class. This form class manages various inputs as well as their validation.

As an example, a basic form will be created below.

If using the Zend skeleton application, ensure that you have a custom module set up. To can find out how to add a module, see the Add a Module in Zend Framework 3 post.

By default the skeleton application might not have the zend-form package installed, so make sure that you require the package and packages that the form component requires.

$ composer require zendframework/zend-form
$ composer require zendframework/zend-i18n
$ composer require zendframework/zend-math
$ composer require zendframework/zend-session

Any forms created should belong in the Form directory of your module. For example, within a Custom, a RegisterForm.php is created.

// module/Custom/src/Form/RegisterForm.php
<?php
namespace Custom\Form;

use Zend\Form\Form;

class RegisterForm extends Form
{
    
}

As mentioned earlier, form elements are added using the $this->add() method. This method is executed within the class’ __construct() method.

// module/Custom/src/Form/RegisterForm.php
<?php
namespace Custom\Form;

use Zend\Form\Form;

class RegisterForm extends Form
{
    public function __construct()
    {
        parent::__construct();
        $this->setAttribute('method', 'POST');

        $this->add([
            'type'  => 'text',
            'name' => 'first_name',
            'attributes' => [
                'id'  => 'first_name',
            ],
            'options' => [
                'label' => 'First Name',
            ],
        ]);

        $this->add([
            'type'  => 'text',
            'name' => 'last_name',
            'attributes' => [
                'id'  => 'last_name',
            ],
            'options' => [
                'label' => 'Last Name',
            ],
        ]);

        $this->add([
            'type'  => 'text',
            'name' => 'email_address',
            'attributes' => [
                'id'  => 'email_address',
            ],
            'options' => [
                'label' => 'Email Address',
            ],
        ]);

        $this->add([
            'type'  => 'password',
            'name' => 'password',
            'attributes' => [
                'id'  => 'email_address',
            ],
            'options' => [
                'label' => 'Password',
            ],
        ]);
    }
}

Any form you add should require protection against CSRF (Cross-Site Request Forgery). Zend Framework provides a CSRF element to add to your form model.

$this->add([
  'type'  => 'csrf',
  'name' => 'csrf',
  'options' => [                
    'csrf_options' => [
      'timeout' => 600
    ]
  ],
]);

And finally, add a submit element to post the form data.

$this->add([
    'name'       => 'submit',
    'type'       => 'submit',
    'attributes' => [
        'value' => 'Submit',
        'id'    => 'submit',
    ],
]);

In order to add the form to your view template, within your module’s controller class, include your form class using the use keyword near the top of the class, instantiate a new instance of the form class, and pass this instance as an argument into the ViewModel instance.

// module/Custom/src/Controller/IndexController.php
<?php
namespace Custom\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Custom\Form\RegisterForm;

class IndexController extends AbstractActionController
{
    public function indexAction()
    {
        $formModel = new RegisterForm();
        return new ViewModel(array('form' => $formModel));
    }
}

As the key passed into the ViewModel instance is called form, you can access the form within the view template using the $form variable.

// module/Custom/view/custom/index/index.phtml
<?php echo $this->form()->render($form);

Create a Form in Zend Framework 3

Currently the form is rendered horizontally, however you can use CSS rules to shape the form to your needs.