donate Buy us a cup of coffee
Our service is free. But your donation can help us keep running. To rent a server and domain, to buy us a cup of coffee, to reduce our activity in paid stuff and make this service better. You can send us a donation to BCA Bank, 008 090 8440 in the name of Abdul Malik Ikhsan with Swifcode CENAIDJA. Please confirm to samsonasik@gmail.com after that ;).
download Download module      



Form with Zend\Form sample usage

This example contains about How to use Form with Zend\Form in Zend framework 2 codes, This demo demonstrate to use Zend\Form via Form class that extends Zend\Form use LearnZF2Form like below :

LearnZF2FormUsage\Form\LearnZF2Form
        
namespace LearnZF2FormUsage\Form;

use Zend\Form\Form;
use Zend\InputFilter\InputFilterProviderInterface;

class LearnZF2Form extends Form implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('formname');
    }

    public function init()
    {
        $this->setAttribute('method', 'post');

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

        $this->add([
            'name' => 'name',
            'attributes' => [
                'type'  => 'text',
                'class' => 'form-control',
            ],
            'options' => [
                'label' => 'Name',
            ],
        ]);

        $this->add([
            'type' => 'Zend\Form\Element\Select',
            'name' => 'gender',
            'options' => [
                'label' => 'Gender',
                'value_options' => [
                    '1' => 'Select your gender',
                    '2' => 'Female',
                    '3' => 'Male',
                ],
            ],
            'attributes' => [
                'class' => 'form-control',
                'value' => '1', //set selected to '1'
            ],
        ]);

        $this->add([
            'type' => 'Zend\Form\Element\MultiCheckbox',
            'name' => 'hobby',
            'options' => [
                'label' => 'Please choose one/more of the hobbies',
                'value_options' => [
                    '1' => 'Cooking',
                    '2' => 'Writing',
                    '3' => 'Others',
                ],
            ],
            'attributes' => [
                'class' => 'form-control',
                'value' => '1', //set checked to '1'
            ],
        ]);

        $this->add([
            'type' => 'Zend\Form\Element\Email',
            'name' => 'email',
            'options' => [
                'label' => 'Email',
            ],
            'attributes' => [
                'placeholder' => 'you@domain.com',
            ],
        ]);

        $this->add([
            'type' => 'Zend\Form\Element\Date',
            'name' => 'birth',
            'options' => [
                'label' => 'Birth ( Y/m/d )',
            ],
            'attributes' => [
                'class' => 'form-control',
            ],
        ]);

        $this->add([
            'name' => 'address',
            'attributes' => [
                'class' => 'form-control',
                'type' => 'textarea',
            ],
            'options' => [
                'label' => 'Address',
            ],
        ]);

        $this->add([
            'type' => 'Zend\Form\Element\Radio',
            'name' => 'direction',
            'options' => [
                'label' => 'Please choose one of the directions',
                'value_options' => [
                    '1' => 'Programming',
                    '2' => 'Design',
                ],
            ],
            'attributes' => [
                'class' => 'form-control',
                'value' => '1', //set checked to '1'
            ],
        ]);

        $this->add([
            'name' => 'submit',
            'attributes' => [
                'class' => 'form-control',
                'type'  => 'submit',
                'value' => 'Go',
                'id' => 'submitbutton',
            ],
        ]);
    }

    public function getInputFilterSpecification()
    {
        return [
            [
                'name'     => 'id',
                'required' => false,
                'allow_empty' => true,
                'filters'  => [
                    ['name' => 'Int'],
                ],
            ],
            [
                'name'     => 'name',
                'required' => true,
                'filters'  => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'encoding' => 'UTF-8',
                            'min'      => 5,
                            'max'      => 255,
                        ],
                    ],
                ],
            ],
            [
                'name'     => 'gender',
                'validators' => [
                    [
                        'name'    => 'InArray',
                        'options' => [
                            'haystack' => [2,3],
                            'messages' => [
                                'notInArray' => 'Please select your gender !',
                            ],
                        ],
                    ],
                ],
            ],
            [
                'name'     => 'hobby',
                'required' => true
            ],
            [
                'name'     => 'email',
                'validators' => [
                    [
                        'name'    => 'EmailAddress',
                    ],
                ],
            ],
            [
                'name'     => 'birth',
                'validators' => [
                    [
                        'name'    => 'Between',
                        'options' => [
                            'min' => '1970-01-01',
                            'max' => date('Y-m-d'),
                        ],
                    ],
                ],
            ],
            [
                'name'     => 'address',
                'required' => true,
                'filters'  => [
                    ['name' => 'StripTags'],
                    ['name' => 'StringTrim'],
                ],
                'validators' => [
                    [
                        'name'    => 'StringLength',
                        'options' => [
                            'encoding' => 'UTF-8',
                            'min'      => 5,
                            'max'      => 255,
                        ],
                    ],
                ],
            ],
            [
                'name'     => 'direction',
                'required' => true
            ]
        ];
    }
}

    
We can define elements in init() function, and define filters and validations in getInputFilterSpecification function.

By the way, you can find other examples using Zend Framework 2 in our home page :)