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?