A free and open-source book on Zend Framework for beginners


10.3. File Uploads & ZF3 Form Model

To add file uploading capability to your form model, you need to add an element of the Zend\Form\Element\File class as follows:

    // Add the following code inside of form's addElements() method.
  
    // Add the "file" field.
    $this->add([
        'type'  => 'file',
        'name' => 'file',
        'attributes' => [                
            'id' => 'file'
        ],
        'options' => [
            'label' => 'Upload file',
        ],
    ]);

In the code above, we call the add() method provided by the Form base class and pass it the configuration array describing the element. The type key of the array (line 5) must be either Zend\Form\Element\File class name or its short alias "file".


Top