2

I am implementing an updatePasswordAction and its not displaying an error with an invalid current password. I could not implement this with a Zend_Validate class to use with a supplied record->password, so i just validated for now in my controller action and if failed then i add the error message to the form element. this is just before i run $form->isValid. In any case, its working. but when the validation fails, its not displaying the error message on this on the element. any help would be greatly appreciated.

FYI: When I submit a blank current password, it shows the validation

class Admin_Form_UserPassword extends Katana_Form
{
    public function init()
    {
    $element = $this->createElement('hidden', 'id');
    $this->addElement($element);

    $element = $this->createElement('password','password');
    $element->setLabel('Current Password:');
    $element->setRequired(true);        
    $this->addElement($element);

    $element = $this->createElement('password','new_password');     
    $element->setLabel('New Password:');
    $element->addValidator('StringLength', false, array(6,24));     
    $element->setRequired(true);
    $element->addValidator('NotEmpty');     
    $this->addElement($element);

    $element = $this->createElement('password','new_password_confirm');
    $element->setLabel('Confirm:');
    $element->addValidator('StringLength', false, array(6,24));
    $element->addValidator('IdenticalField', false, array('new_password', 'Confirm Password'));
    $element->setRequired(true);
    $this->addElement($element);

    $this->addElement('submit', 'submit', array('label' => 'Submit'));
}

}

public function updatePasswordAction()
{
    $resourceModel  = new Core_Model_Resource_User();       
    $form           = new Admin_Form_UserPassword();
    $form->setMethod(Katana_Form::METHOD_POST);
    $form->setAction($this->getActionUrl('update-password'));
    if($this->getRequest()->isPost()){
        $id             = $this->getRequest()->getParam('id');
        $record         = $resourceModel->find($id)->current();
        $currPassword   = $record->password;
        $typedPassword  = md5($this->getRequest()->getParam('password'));           
        if($currPassword !== $typedPassword){               
            $form->getElement('password')->addError('Current password is incorrect.');
        }
        if($form->isValid($_POST)){
            $data       = $form->getValues();
            $result     = $resourceModel->updatePassword($id, $data['new_password']);
            if($result){
                $this->redirectSimple('list');
            }
        }
    } else {
        $id     = $this->getRequest()->getParam('id');          
        $recordData = array(
            'id' => $id
        );
        $form->populate($recordData);           
    }
    $this->getView()->form = $form;     
}
7
  • What do you mean by 'not working'? Are there some errors? Nothing happens? etc.. Commented Jan 23, 2012 at 20:14
  • When I submit an invalid currentpassword, its not displaying the error message. it should, as within the updatePasswordAction, i use $form->getElement('password')->addError('Current password is incorrect.') just before i run if($form->isValid($_POST)){ Commented Jan 23, 2012 at 20:17
  • Zend_Form_Element::addError() is additional function and you must use Zend_Form::getErrorMessages() to retrieve them. Do you use getErrorMessagess()? Commented Jan 23, 2012 at 20:25
  • Shouldnt me setting the error before isValid cause the form to be invalidated and automatically set the error message for the current password element. It does this when the element is blank, so it should do it when the currentpassword is incorrect. Commented Jan 23, 2012 at 20:28
  • Do you get the mesage Current password is incorrect when element is blank? Commented Jan 23, 2012 at 20:32

2 Answers 2

6

Adding an error to the element doesn't cause the form itself to then be invalid.

There are at least 2 methods I use to get around this:

if($currPassword !== $typedPassword){               
    $form->getElement('password')->addError('Current password is incorrect.');
    $form->markAsError();
}

// or

if ($form->isValid($_POST) && 0 == sizeof($form->getMessages()) {
    // form was valid, and no errors were set on elements
}

To clarify, when you add the error to the form ELEMENT, there is an error attached to that element, but Zend_Form::isValid only runs the validators and sets appropriate errors, it doesn't check to see if you had set an error on a particular element.

You can however call $form->getMessages() to get all the error messages attached to the form or its child elements. If this is 0 and you have validated your form, then it means there were no errors. If your form passed isValid but you added an error to an element, it will include the error message you added.

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

Comments

0

I made it work this way.

Controller:

if ($trial->getKind() != 'debt' && $_POST['kind'] == 'debt')
{
    $editForm->getElement('kind')->markAsError();
}

if ($editForm->isValid($_POST)) { ... }

Form:

public function isValid($data)
{
    $valid = parent::isValid($data);

    if ($this->getElement('kind')->hasErrors()) {
        $this->getElement('kind')->addError($this->_translate->translate('You can\'t change trial kind to debt.'));
        $valid = false;
    }

    return $valid;
}

And this comment helped me.

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.