1
votes

I have some validators set up in a Zend 2 form, but isValid always returns true, ignoring them. Dumping the whole form object, it doesn't look like the validators are even attached, here's the form code:

namespace UserManagement\Form;
use Zend\Form\Form;

class SearchUserForm extends Form
{
    public function __construct($name = null)
    {
        // we want to ignore the name passed
        parent::__construct('SearchUser');
        $this->setAttribute('method', 'post');
        $this->add(array(
            'name' => 'search',
            'attributes' => array(
                'type'  => 'text',
                'required' => true,
            ),
            'filters'  => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                    'name'    => 'StringLength',
                    'options' => array(
                        'encoding' => 'UTF-8',
                        'min'      => 4,
                        'max'      => 100,
                    ),
                )
            ),
        ));

Then in the controller I check if it's valid:

        if( $this->getRequest()->isPost() )
        {

            $searchForm->setData( $this->params()->fromPost() );
            if( $searchForm->isValid() )
            {
                echo "yep";exit;
            }
            else
            {
                echo "nope";exit;
            }

Always outputs 'yep' despite a 1 character string length. I have actually got this working but placing the validators in a separate filter class and attaching it to the form instead - but my question is should this work?

1
Adding fields to the form and setting the filter-validations to them are two different things. Check this for reference - framework.zend.com/manual/2.3/en/user-guide/… You can always set the input filters in the Form class itself but its definition should be separate to adding fields to the form. - Kunal Dethe

1 Answers

1
votes

No I don't think what you are doing will ever work, as you say you can use a separate input filter. You can also use the InputFilterProviderInterface on the form as below

<?php
namespace Test\Form;

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

class TestForm extends Form implements InputFilterProviderInterface
{    
/**
 * Provide default input rules for this element
 *
 * Attaches strip tags filter
 *
 * @return array
 */
public function getInputFilterSpecification()
{
    return [
        'search' => [
            'required' => true,
            'filters'  => array(
            array('name' => 'StripTags'),
            array('name' => 'StringTrim'),
        ),
        'validators' => array(
            array(
                'name'    => 'StringLength',
                'options' => array(
                    'encoding' => 'UTF-8',
                    'min'      => 4,
                    'max'      => 100,
                ),
            )
        ),
        ]
    ];
}



public function __construct()
{
    $this->add(array(
            'name' => 'search',
            'type' => 'Text',

    ));
}

}