开发者

Zend framework multiple Validators in an array

开发者 https://www.devze.com 2022-12-28 15:14 出处:网络
I want to create a form in Zend framework. I am using the code below for a f开发者_高级运维ield:

I want to create a form in Zend framework. I am using the code below for a f开发者_高级运维ield:

$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


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


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

精彩评论

暂无评论...
验证码 换一张
取 消