views:

112

answers:

2

Hello everybody

I want to create a form in Zend framework. I am using the code below for a field:

$this->addElement('text', 'username', array(
    'label'      => 'Username:',
    'required'   => true,
    'filters'    => array('StringTrim'),
    'validators' => array(
        'alnum'
    )
));

This works. But now I also want to add a new validator. In this case StrinLength

$element->addValidator('StringLength', false, array(6, 20));

How can I add this validator in the array I already have? Tnx in advanced

+2  A: 

Doesn't this work:

<?PHP
$this->addElement('text', 'username', array(
    'label'      => 'Username:',
    'required'   => true,
    'filters'    => array('StringTrim'),
    'validators' => array(
        'alnum',
        array('StringLength', false, array(6,20))
    )
));

Similar to the example given in the manual

timdev
I'll try it when I get home :). Tnx for your replay, I'll let you know How it works out!
iSenne
A: 

You can specify the names of arguments to the addValidator() method as array keys:

$this->addElement('text', 'username', array(
    'label'      => 'Username:',
    'required'   => true,
    'filters'    => array('StringTrim'),
    'validators' => array(
        'alnum',
        // See below
        array(
            'validator'     => 'StringLength',
            'options'       => array(6, 20)
        )
    )
));
smack0007