开发者

Having issues with Zend Validation (Zend_Validate_Db_NoRecordExists)

I'm setting up a user registration page, and am validating on the username field which checks firstly if the user doesn't already exist, secondly if the username isn't a reserved one. My problem is when I attach the two validators to the form element it ignores the second validation. The second validation will only work if the first validation is removed. Is this a bug? If so, how can I fix it?

Below are the validators:

    $validator = new Zend_Validate_Db_NoRecordExists(
                    array(
                        'table' => 'users',
                        'field' => 'username'
                    )
    );
    $validator->setMessage('Username %value% already exists', Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND);

    $reserved_validator = new Zend_Validate_Db_NoRecordExists(
                    array(
                        'table' => 'reserved_users',
                        'field' => 'name'
                    )
    );
    $reserved_validator->setMessage('Username %value% is not available', Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND);

Then on the element I have :

            ->addValidator($validator)
            ->addValidator($reserv开发者_JAVA技巧ed_validator)


There is an issue when using two validators of same type. What you need to do is add a Validator Chain:

$validatorChain = new Zend_Validate();
$validatorChain->addValidator($validator)                            
               ->addValidator($reserved_validator);

And just add the chain to the element.

->addValidator($validatorChain);


No this is not a bug, this is by design, I'd say. You can have multiple types of validators but only one type each. You have to look at the DB validator if it allows joining tables. I've never use DB validators so far.

Update Just noticed the other answer, that chain should do the trick.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜