0

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 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

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

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)
        )
    )
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.