Zend3 InputFilter not validating Form

66 Views Asked by At

I've found another post (ZF2 InputFilter not validating fieldset) for my issue, but it doesn't helped.

I have a Category Entity and i want to validate the length of the title. So my Model (without the existing getter and setter)

class Category
{
    /**
     * @var int
     */
    private $id;

    /**
     * @var string
     */
    private $name;
}

My Form for the Category:

class CategoryForm extends Form
{

    public function init()
    {

        $this->setHydrator(new ClassMethods(false));
        $this->setObject(new Category());
        $this->setInputFilter(new CategoryFilter());

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

        $this->add([
            'name' => 'name',
            'type' => 'text'
        ]);

        $this->add([
            'name' => 'submit',
            'type' => 'submit'
        ]);
    }

}

And the Filter which currently not working.

class CategoryFilter extends InputFilter
{
    public function init()
    {
        $this->add([
            'name' => 'name',
            'required' => true,
            'filters' => [
                ['name' => StringTrim::class]
            ],
            'validators' => [
                [
                    'name' => StringLength::class,
                    'options' => [
                        'min' => 5
                    ]
                ]
            ]
        ]);
    }
}

And if someone needed my addAction in the Controller:

public function addAction()
{
    $request = $this->getRequest();

    if ($request->isPost()) {
        $this->form->setData($request->getPost());

        if ($this->form->isValid()) {
            $this->mapper->save($this->form->getData());

            $this->redirect()->toRoute('categories');
        }
    }

    return [
        'form' => $this->form
    ];
}

In every example which i found, it should be working. But my form is never validated nor filtered (with the trim).

Did i forget something? Why it doesn't work?

1

There are 1 best solutions below

2
On BEST ANSWER

So i've changed a little bit.

The Category Form goes to:

class CategoryForm extends Form
{

    public function init()
    {
        $this->add(array(
            'type' => CategoryFieldSet::class,
            'options' => array(
                'use_as_base_fieldset' => true,
            ),
        ));

        $this->add([
            'name' => 'submit',
            'type' => 'submit'
        ]);
    }
}

I changed also the Filter

class CategoryFilter extends InputFilter
{
    public function __construct()
    {
        $this->add([
            'name' => 'name',
            'required' => true,
            'filters' => [
                ['name' => StringTrim::class]
            ],
            'validators' => [
                [
                    'name' => StringLength::class,
                    'options' => [
                        'min' => 5
                    ]
                ]
            ]
        ]);
    }
}

And after this i defined the FieldSet with the validators:

class CategoryFieldSet extends Fieldset implements InputFilterProviderInterface
{
    /**
     *
     */
    public function init()
    {
        $this->setHydrator(new ClassMethods(false));
        $this->setObject(new Category());

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

        $this->add([
            'name' => 'name',
            'type' => 'text',
        ]);
    }

    /**
     * Should return an array specification compatible with
     * {@link Zend\InputFilter\Factory::createInputFilter()}.
     *
     * @return array
     */
    public function getInputFilterSpecification()
    {
        $filter = new CategoryFilter();

        return $filter->getInputs();
    }
}

After changing this i get the expected error message like:

The input is less than 5 characters long