Hi All,

I feel like I'm taking crazy pills here - spending way too much time trying
to figure out this (probably quite simple) problem that I'm encountering.

I have created a class which extends Zend_Form.  It looks like this:

class UserForm extends Zend_Form{
  protected $_purposeCodes = array('create', 'view', 'edit'); 
  /**
   * Constructor
   * @param User $user - User object for whom the form is being rendered
   * @param string $purpose - Intended purpose for which the form should be
configured (create, view, edit)
   */
  public function __construct(User $user, $purpose){
    parent::__construct();
    $this->createCommonElements();
    $this->applyPurposeModifications($purpose);
    $this->applyRoleModifications($user->getRoleId());
  }
  
  protected function applyPurposeModifications($purpose){
    if(in_array($purpose, $this->_purposeCodes)){
      switch($purpose){
        case 'create':
          $submitElement = $this->getElement('submit');
          $submitElement->setLabel('Create User');
          //$submitElement->setName('Create User');
          break;
        case 'view' :
          $allElements = $this->getElements();
          foreach($allElements as $element){
            $element->setAttrib('disabled', true);
          }
          $submitElement = $this->getElement('submit');
          $submitElement->setLabel('Edit User');
          
          $attribs = $submitElement->getAttribs();
          print 'Attribs before unsetting: <br>';
          print_r($attribs);
          unset($attribs['disabled']);
          print '<br>Attribs after unsetting: <br>';
          print_r($attribs);
          $submitElement->setAttribs($attribs);
                   
          break;
      }
    }
  }
  
  protected function applyRoleModifications($role){
    switch($role){
      case 'admin' :
        break;
      case 'manager' :
        $userRoleElement = $this->getElement('user_role');
        $userRoleElement->removeMultiOption('admin');
        $userRoleElement->removeMultiOption('manager');
        break;
    }
  }
  
  protected function createCommonElements(){
    $this->addElement('Select', 'user_role', array(
      'label' => 'User Role/Permissions',
      'MultiOptions' => array(
        ''                => '',
        'admin'   => 'Administrator',
        'manager' => 'Manager',
        'mentor'  => 'Mentor'
      ),
    ));
    $this->addElement('text', 'user_username', array(
      'label' => 'Email (Used for Login)',
      'required' => true,
    ));
    $this->addElement('text', 'user_first', array(
      'label' => 'First Name',
      'required' => true
    ));
    $this->addElement('text', 'user_last', array(
      'label' => 'Last Name',
      'required' => true
    ));
    $this->addElement('submit', 'submit', array(
      'label' => 'Submit Button'
    ));
    
    /*
    $this->setDecorators(array(
      'ViewHelper',
      'Errors',
      array('Description', array('placement' => 'prepend')),
      array('HtmlTag', array('tag' => 'div')),
      array('Label', array('tag' => 'span'))
    ));
        */
  }
}

What I can't seem to figure out is how to re-enable an element after it has
been disabled.  This is done in applyPurposeModifications - I loop through
the elements to disable them all, then I need to re-enable the submit button
- I just can't figure it out... Help would be greatly appreciated!

Thanks in advance,
Erik
-- 
View this message in context: 
http://www.nabble.com/Zend-Form-Remove-or-Unset-Attribute-tp20868239p20868239.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to