Zendframework 3 – Create a simple form

January 29, 2018

In this tutorial, we will see how to create a simple form using Zend framework 3. To create a form in zendframework you have to install Zend-form component. You can install it using following composer command in your terminal. So you go the root of your project and run the command

composer require zendframework/zend-form

You need zend-i18n to render the form in your view

composer require zendframework/zend-i18n

You can create the following for at module\Application\src\Form\ProductForm.php

setAttribute('method', 'post');
    // (Optionally) set action for this form
    $this->setAttribute('action', '/products/create');
    // Adding Elements

    $elementName = new Text(
                'name',                // Name of the element
                [                     // Array of options
                 'label'=> 'Product Name'  // Text label
    $elementName->setAttribute('id', 'name');

    // Add the "name" field to the form


    $elementPrice = new Text('price', [ 'label'=> 'Product Price' ]);
    // Add the "price" field to the form

    // Submit button
    $elementSubmit = new Submit('submit');

    $elementSubmit->setAttribute('value', 'Submit');


I have created two Text fields and Submit button in this code

Now you can create the ProductForm object and pass it into the ViewModel as an argument. So you can place the following code in your controller

public function createAction()
        return new ViewModel([
        		'form'=>new \Application\Form\ProductForm()

Now you have the ProductForm object inside your ViewModel and you can add following code to view file at

echo $this->form()->render($form);

Now you can see this kind of simple form