C
C
Cat Anton2014-11-28 14:07:23
Zend Framework
Cat Anton, 2014-11-28 14:07:23

How to set allow_empty on a form element using a factory in ZF2?

There is an element that has by default (allow_empty == true):

namespace Application\Form\Element\Text;

use Zend\Form\Element\Text;
use Zend\InputFilter\InputProviderInterface;
use Zend\Validator\NotEmpty;

class Login extends Text implements InputProviderInterface
{
    public function getInputSpecification()
    {
        return array(
            'name' => 'login',
            'required' => true,
            'allow_empty' => true,
            'validators' => array(
                array(
                    'name' => 'notEmpty',
                ),
            ),
        );
    }
}

When creating a form with this element using a factory, you need to set (allow_empty == false). The only way I've found requires you to know the name of the element, and that doesn't work:
$form = $factory->createForm(array(
    'elements' => array(
        array(
            'spec' => array(
                'type' => 'Application\Form\Element\Text\Login',
            ),
        ),
    ),
    'input_filter' => array(
        array(
            'name' => 'login',
            'allow_empty' => false,
        ),
    ),
));

For some reason, this method does not work as intended:
$form = $factory->createForm(array(
    'elements' => array(
        array(
            'spec' => array(
                'type' => 'Application\Form\Element\Text\Login',
                'allow_empty' => false,
            ),
        ),
    ),
));

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Shirshov Alexander, 2014-12-06
@27cm

The form element does not have the allow_empty property, it is the Input property (which is stored in the image and likeness of the form in the InputFilter), and is used there.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question