How to set a Form attribute from controller


   If we have a form used for different purpose and in one of the action you want to set a particular attribute to form object. Then you can do that by setting the attribute value from the controller action page.
   Ex:
     public function addAction()
     {
         $form    =    new Form_User();
         $updateUser=    $form->addUser();
         $updateUser->role->setAttrib('disabled','true');
     }

How to set SMTP mail using Zend Framework

1. First you have to download the Zend Framework library.
      Then place it somewhere in the server with name like library and set the include_path to your working php page
      Ex: set_include_path(get_include_path().PATH_SEPARATOR.realpath(realpath(dirname(__FILE__)).'/library'));
   2. Include Zend_mail  and Zend_Mail_Transport_Smtp file in the file/function
   3. Set/Get the config variables
      Use the following code.
        require_once 'Zend/Mail.php';
     require_once 'Zend/Mail/Transport/Smtp.php';
    
     $config = array('auth' => 'login',
                    'username' => 'info@domainname',
                    'password' => 'password');
               
        $transport = new Zend_Mail_Transport_Smtp('smtpServer', $config);
       
    $mail = new Zend_Mail();
   
    $mail->setFrom('info@domainname', 'DO NOT REPLY');
    $mail->addTo($toEmail, 'Recipient');
    $mail->setSubject('Set Subject');
    $mail->setBodyText('Add Message');
   
    $mail->send($transport);

Zend file upload error - Exceeds the defined ini size

This error can occur due to the following reasons.
       1. Your upload image size is greater than the maximum upload size.
       2. You have not set the attribute enctype=multipart/form-data in the form tag.

fileUploadErrorAttack - File was illegally uploaded. This could be a possible attack

  It occurs when you try to get the File upload data after the Form getValues() function.
  First you have to add code for File upload data then the form values.

  Ex:
     if ($this->getRequest()->isPost()) {
       if($this->formObj->isValid($_POST)) {

               //Form upload section
                   $upload = new Zend_File_Transfer_Adapter_Http();
           
        $upload->addValidator('Count', false, array('min' =>0, 'max' => 1))
                ->addValidator('IsImage', false, 'jpeg,pjpeg,jpg,png,gif')
                  ->addValidator('Size', false, array('max' => '10MB'))
                       ->setDestination('upload_path');

                 try {
               
             $upload->receive();
                    
           } catch (Zend_File_Transfer_Exception $e) {
     
             die('ERROR'.$e->getMessage());
         }
              //Get form values

              $postValues = $this->formObj->getValues();

          }
      }

How to set value for multiple select options in Zend_Form_Element_Multiselect

We can do that using the setValue function by passig array of selected values
Ex:
    $users = new Zend_Form_Element_Multiselect("users");
    $users->setLabel("Users");
    $users->addMultiOptions(array('1'=>'John Smith','2'=>'Aaron Brown','3'=>'David Beckham');
    $users->setValue(array(1,2));
    so the names John and Aaron will be shown as selected.

Disable HTML Select Option in Zend Form

We can do that using the setAttribs function in the Zend Form Element

   Ex:
      $roles = new Zend_Form_Element_Select("roles");
      $roles->setLabel("Roles");
      $roles->addMultiOptions(array('1' => 'Admin', '2'=>'Group Admin','3'=>'User'));
      suppose if we want to set it disable if the user is 'User'.
          if($roleValue > 2)
        $roles->setAttribs(array('disabled'=>'disabled'));

How to pass variable with _redirector helper zend framework

$this->_helper->_redirector('action','controller','module',array('param1'=>'value1'));