如何使用相同的字段验证以zend形式添加和编辑操作?

时间:2012-09-23 13:17:25

标签: php zend-framework zend-form zend-validate

我是zend框架的新手。我正在开发一个简单的用户注册应用程序在zend。我在我的应用程序中添加和编辑功能。我可以使用我的应用程序完美地添加新用户。但是当我尝试编辑除密码以外的用户信息时,用于密码和密码确认字段的验证器会出错。只有当我想编辑除密码以外的用户信息时,我才需要删除/禁用密码和密码确认字段的验证器。

我的表格:

$password = new Zend_Form_Element_Password('password');
        $password->setRequired(true)
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->addValidator('NotEmpty', false, array('messages'=>'password cannot be empty'))
        ->addValidator('StringLength', false, array(2, 20, 'messages'=>'password must be 2-20 character'))
        ->setLabel('Password:');
        $this->addElement($password);

        $confirmPassword = new Zend_Form_Element_Password('confirmPassword');
        $confirmPassword->setRequired(true)
        ->addFilter('StringTrim')
        ->addFilter('StripTags')
        ->addValidator('NotEmpty', false, array('messages'=>'password do not match'))
        ->addValidator(new Application_Validate_PasswordConfirmation())
        ->setLabel('Retype password');

我的验证员类:

class Application_Validate_PasswordConfirmation extends Zend_Validate_Abstract
{
     const NOT_MATCH = 'notMatch';

     protected $_messageTemplates = array(
     self::NOT_MATCH => 'Password confirmation does not match'
     );

     public function isValid($value, $context = null)
     {
         $value = (string) $value;
         $this->_setValue($value);

         if (is_array($context)) {
            if (isset($context['password']) && ($value == $context['password']))
            {
                return true;
            }
         } elseif (is_string($context) && ($value == $context)) {
                return true;
           }

         $this->_error(self::NOT_MATCH);
         return false;
     }
}

2 个答案:

答案 0 :(得分:4)

    $modelUsers = new Model_Users();
    $userId = $this->_getParam('userId');

    if ($userId) {
        $populateData = array();

        $user = $modelUsers->fetch($userId);

        if ($user instanceof Model_User) {
            $populateData = $user->toArray();
        }
        $form = $this->_getAddForm($user->email, $user->username, $user->phone);
        $password = $this->getRequest()->getPost('password');
        if (trim($password) == '') {
            $form->getElement('password')
            ->setRequired(false)
            ->clearValidators();

            $form->getElement('confirmPassword')
            ->setRequired(false)
            ->clearValidators();
        }

    }
   else {
        $form = $this->_getAddForm($mail=null, $username=null, $phone=null);
   }
    $request = $this->getRequest();

    if ($request->isPost()) {

        $post = $request->getPost();
        if ($form->isValid($post)) {
            $values = $form->getValidValues($post);

这就是我在@ drew010

的帮助下绝对解决这个问题的方法

答案 1 :(得分:3)

在以编辑模式提交表单时,请在调用$form->isValid()之前使用以下代码删除验证程序:

$password = $this->getRequest()->getPost('password');
if (trim($password) == '') { // password field empty, remove validators
    $form->getElement('password')
         ->setRequired(false)
         ->clearValidators();

    $form->getElement('confirmPassword')
         ->setRequired(false)
         ->clearValidators();
}

if ($form->isValid($this->getRequest()->getPost())) {
    // ...
}