Krizsán Balazs
Krizsán Balazs

Reputation: 385

ZF2 form as array

I'd like to create a simple zend form with a few fields but i wanna collect this fields into an array. I'd like to see my form names like this:

name="login[username]" name="login[password]" name="login[submit]"

I wasn't able to find any description. If somebody knows the solution please let me know!

Upvotes: 1

Views: 1771

Answers (1)

Remi Thomas
Remi Thomas

Reputation: 1528

You can try with fieldsets like that

namespace Application\Form;

use Application\Entity\Brand;
use Zend\Form\Fieldset;
use Zend\InputFilter\InputFilterProviderInterface;

class YourFieldset extends Fieldset implements InputFilterProviderInterface
{
    public function __construct()
    {
        parent::__construct('login');

        $this->add(array(
            'name' => 'username',
            'options' => array(
                'label' => 'Username'
            ),
            'attributes' => array(
                'required' => 'required'
            )
        ));

        $this->add(array(
            'name' => 'password',
            'type' => 'Zend\Form\Element\Password',
            'options' => array(
                'label' => 'Password'
            ),
            'attributes' => array(
                'required' => 'required'
            )
        ));

    $this->add(array(
            'name' => 'submit',
            'type' => 'Zend\Form\Element\Submit',
            'options' => array(
                'label' => 'Submit'
            ),
            'attributes' => array(
                'required' => 'required'
            )
        ));
    }

    /**
     * @return array
     */
    public function getInputFilterSpecification()
    {
        return array(
            'name' => array(
                'required' => true,
            )
        );
    }
}

Upvotes: 3

Related Questions