Skip to main content

Zend Form with Decorator Class

you have to make some changes in this code according to you need but it wil help you alot....

1: View:

<?php echo $this->food; ?>

<?php echo $this->cab; ?>


2:Controller:



 public function indexAction(){   


  $ReimbursermentFood=new Form_Webchutney( array('selectForm' => '__FOOD_REIMBURSEMENT'));
  $ReimbursermentCab=new Form_Webchutney( array('selectForm' => '__CAB_REIMBURSEMENT'));
$Employeearray=array();  
 


  $Employeearray['AccountManager']='Account Manager';

  $Employeearray['aktel']='aktel';
  $Employeearray['saurabh.patel']='saurabh patel';

  $ReimbursermentFood->setAction('/user/reimbursement/food');
  $ReimbursermentFood->setAttrib("id","foodForm");

  $AccountManager = $ReimbursermentFood->getElement('AccountManager');

  $AccountManager->setLabel('')
                ->addMultiOptions($Employeearray); 
   #$AccountManager->setValue("Aditya Kumar");
   $AccountManager->setAttrib('readonly', 'readonly');
  $this->view->Food=$ReimbursermentFood;


  $ReimbursermentCab->setAction('/user/reimbursement/cab');    
  $ReimbursermentCab->setAttrib("id","cabForm");

  $AccountManager = $ReimbursermentCab->getElement('AccountManager');
  #$AccountManager->setValue("Aditya Kumar");
   $AccountManager->setLabel('')
                ->addMultiOptions($Employeearray); 
   $AccountManager->setAttrib('readonly', 'readonly');
  #$ReimbursermentCab->removeElement("Amount");    
$this->view->Cab=$ReimbursermentCab;


 
}





3:Form:
_________________________________________

<?php

class Form_Aktel extends Zend_Form {
   

    /* @access Public
     * @var object
     */
   
    
     /* @access Public
     * @var object
     */
  

    public $REInput;
    public $REButton;

    
    protected $selectForm = '';

    public function setselectForm($param)  {
            $this->selectForm = $param;           
            
        } 

    
    private function decorator(){
 
                $this->REInput= New Form_Decorator_ReInput();
      $this->REButton= New Form_Decorator_ReButton(); 
      $this->RESETButton= New Form_Decorator_ResetButton();
      
 
  }


    
public function init() {
       
      $this->decorator(); 

 switch ($this->selectForm) 
            {  
            


          

            CASE '__FOOD_REIMBURSEMENT':

                $this->foodForm();
 
                break;    
 
CASE '__CAB_REIMBURSEMENT':

                $this->cabForm();
 
                break;     

}
 
    }
  
    private function foodForm()    {       
         
           
         
         $this->setMethod('post')  ;

        // date
$accountDate = $this->createElement('text', 'date');
$accountDate->setValue('Date');
$accountDate->setRequired(TRUE);
$accountDate->setAttrib('size', 30);
$accountDate->setDecorators(array($this->REInput)) ;
$accountDate->setAttrib('id','food_datepicker');
$accountDate->setAttrib('Class','date');


       // name
     $accountName = $this->createElement('text', 'Name');
$accountName->setValue('Name');
$accountName->setRequired(TRUE);
$accountName->setAttrib('size', 30);
        $accountName->setDecorators(array($this->REInput)) ;
        $accountName->setAttrib('id','food_Name');
$accountName->setAttrib('Class','txtfld');

// Project
     $accountProject = $this->createElement('text', 'Project');
$accountProject->setValue('Project');
$accountProject->setRequired(TRUE);
$accountProject->setAttrib('size', 30);
        $accountProject->setDecorators(array($this->REInput)) ;
        $accountProject->setAttrib('id','food_Project');
$accountProject->setAttrib('Class','txtfld');

// AccountManager
    /* 
     $accountAccountManager = $this->createElement('text', 'AccountManager');
$accountAccountManager->setValue('Account Manager');
$accountAccountManager->setRequired(TRUE);
$accountAccountManager->setAttrib('size', 30);
        $accountAccountManager->setDecorators(array($this->REInput)) ;
        $accountAccountManager->setAttrib('id','food_AccountManager');
$accountAccountManager->setAttrib('Class','txtfld');  
*/

        $accountAccountManager = new Zend_Form_Element_Select('AccountManager');
        


// Punch_time
     $accountPunch_time =$this->createElement('text', 'Punch_time');
$accountPunch_time->setValue('Punch Time');
$accountPunch_time->setRequired(TRUE);
$accountPunch_time->setAttrib('size', 30);
        $accountPunch_time->setDecorators(array($this->REInput)) ;
        $accountPunch_time->setAttrib('id','food_Punch_time');
$accountPunch_time->setAttrib('Class','time');

// Order_time
     $accountOrder_time =$this->createElement('text', 'Order_time');
$accountOrder_time->setValue('Order Time');
$accountOrder_time->setRequired(TRUE);
$accountOrder_time->setAttrib('size', 30);
        $accountOrder_time->setDecorators(array($this->REInput)) ;
        $accountOrder_time->setAttrib('id','food_Order_time');
$accountOrder_time->setAttrib('Class','time');


// Amount
     $accountAmount =$this->createElement('text', 'Amount');
$accountAmount->setValue('Billed Amount');
$accountAmount->setRequired(TRUE);
$accountAmount->setAttrib('size', 30);
        $accountAmount->setDecorators(array($this->REInput)) ;
        $accountAmount->setAttrib('id','food_Amount');
$accountAmount->setAttrib('Class','txtfld');
 
      

//submit
$accountSubmit =new Zend_Form_Element_Submit('Submit');
$accountSubmit->setDecorators(array($this->REButton)) ;

 

//submit
$accountButtom =new Zend_Form_Element_Button('Reset');
$accountButtom->setDecorators(array($this->RESETButton));  

 
     
     
   $accountFrom = $this->createElement('hidden', 'from')                 
            ->setValue(rand(1, 9999999999)); 
       
        // CSR
        $accountcsr = $this->createElement('hash', 'no_csr', array( 'salt' => 'unique'));
 
         
       $this->addElements(array($accountDate, $accountName ,$accountProject, $accountAccountManager,  $accountPunch_time, $accountOrder_time ,  $accountAmount  ,    $accountSubmit  , $accountButtom ,  $accountFrom,$accountcsr)); 
     
  $this->setDecorators(array( 'FormElements','Form'));

         
           
}
    


    private function cabForm()    {       
         
           
         
         $this->setMethod('post')  ;

        // date
$accountDate = $this->createElement('text', 'date');
$accountDate->setValue('Date');
$accountDate->setRequired(TRUE);
$accountDate->setAttrib('size', 30);
$accountDate->setDecorators(array($this->REInput)) ;
$accountDate->setAttrib('id','cab_datepicker');
$accountDate->setAttrib('Class','date');


       // name
     $accountName = $this->createElement('text', 'Name');
$accountName->setValue('Name');
$accountName->setRequired(TRUE);
$accountName->setAttrib('size', 30);
        $accountName->setDecorators(array($this->REInput)) ;
        $accountName->setAttrib('id','cab_Name');
$accountName->setAttrib('Class','txtfld');

// Project
     $accountProject = $this->createElement('text', 'Project');
$accountProject->setValue('Project');
$accountProject->setRequired(TRUE);
$accountProject->setAttrib('size', 30);
        $accountProject->setDecorators(array($this->REInput)) ;
        $accountProject->setAttrib('id','cab_Project');
$accountProject->setAttrib('Class','txtfld');

// AccountManager
    /* $accountAccountManager = $this->createElement('text', 'AccountManager');
$accountAccountManager->setValue('Account Manager');
$accountAccountManager->setRequired(TRUE);
$accountAccountManager->setAttrib('size', 30);
        $accountAccountManager->setDecorators(array($this->REInput)) ;
        $accountAccountManager->setAttrib('id','cab_AccountManager');
$accountAccountManager->setAttrib('Class','txtfld'); */

$accountAccountManager = new Zend_Form_Element_Select('AccountManager');
 //Pick_Up_Area for cab

$accountPick_Up_Area =$this->createElement('text', 'Pick_Up_Area');
$accountPick_Up_Area->setValue('Pickup Area');
$accountPick_Up_Area->setRequired(TRUE);
$accountPick_Up_Area->setAttrib('size', 30);
        $accountPick_Up_Area->setDecorators(array($this->REInput)) ;
        $accountPick_Up_Area->setAttrib('id','cab_Pick_Up_Area');
$accountPick_Up_Area->setAttrib('Class','picfrom');


        //Drop_Area for cab
$accountDrop_Area =$this->createElement('text', 'Drop_Area');
$accountDrop_Area->setValue('Drop Area');
$accountDrop_Area->setRequired(TRUE);
$accountDrop_Area->setAttrib('size', 30);
        $accountDrop_Area->setDecorators(array($this->REInput)) ;
        $accountDrop_Area->setAttrib('id','cab_Drop_Area');
$accountDrop_Area->setAttrib('Class','picto');
          
    }




         
           
}
      

     
          
     private function forgotPasswordForm() {

    $this->setMethod('post')
             ->setAttrib('id', 'forgotPassword')
             ->setAttrib('name', 'forgotPassword');
   
             
    $emailInput= $this->createElement('text', 'Email');
    $emailInput->setLabel('Enter your EmailID');
$emailInput->setRequired(TRUE);
$emailInput->setAttrib('size', 30);
$emailInput->setDecorators(array($this->Input)) ;
$emailInput->addValidator('NotEmpty', true, array('messages' => 'Error_Account_Missing_Email_Field' ));

//submit
$accountSubmit =new Zend_Form_Element_Submit('Submit');
$accountSubmit->setLabel('Submit');
$accountSubmit->setValue('Submit');  
    $accountSubmit->setDecorators(array($this->Button)) ;
$accountFrom = $this->createElement('hidden', 'from')                 
           ->setValue(rand(1, 9999999999));
     
       // CSR
   $accountcsr = $this->createElement('hash', 'no_csr', array( 'salt' => 'unique'));
$this->addElements(array($accountUsername,$accountPassword,$accountSubmit,$accountFrom,$accountcsr)); 
   $this->setDecorators(array( 'FormElements','Form'));
    
     
     }

}




4:Decorator:


_________________________________________________________________________________-

<?php
class Form_Decorator_ReSelect extends Zend_Form_Decorator_Abstract
{
protected $_format = '<div></div>';
   public function render($content)
{
$element = $this->getElement();
$name = htmlentities($element->getFullyQualifiedName());
$label = htmlentities($element->getLabel());
$id = htmlentities($element->getId());
$value = htmlentities($element->getValue());
$markup = sprintf($this->_format,$id, $name, $value);
return $markup;
}


}



__________________________________________________________________________________________


<?php
class Form_Decorator_ReInput extends Zend_Form_Decorator_Abstract
{


protected $_format ='<div class="frmbx">
                            <div class="txtbxlft"></div> 
    <div class="txtbxmid"><input type="text"  class="%s"  id="%s" name="%s"  value="%s" /><span>*</span></div> 
                            <div class="txtbxrht"></div> 
                        </div>';

   public function render($content)
{
$element = $this->getElement();

    
$Class=htmlentities($element->getAttrib("Class"));
$id = htmlentities($element->getAttrib("id"));
$name = htmlentities($element->getFullyQualifiedName());
$value = htmlentities($element->getValue());
$markup = sprintf($this->_format, $Class, $id, $name, $value);
return $markup;
}


}

____________________________________________________________________________________________


<?php
class Form_Decorator_ReButton extends Zend_Form_Decorator_Abstract
{
protected $_format ='<div class="frmbxsubmit">
                                              
                          <input id="%s" name="%s"  type="submit" value="" class="submitbtn">
                                                
                      </div>';

   public function render($content)
{
$element = $this->getElement();
$name = htmlentities($element->getFullyQualifiedName());
$label = htmlentities($element->getLabel());
$id = htmlentities($element->getId());
$value = htmlentities($element->getValue());
$markup = sprintf($this->_format,$id, $name);
return $markup;
}


}



Comments

Popular posts from this blog

HTML Emailing in ZF2

<?php namespace Application\Model; use Zend\Mail\Message; use Zend\Mime\Message as MimeMessage; use Zend\Mime\Part as MimePart; use Zend\Mail\Transport\Sendmail as SendmailTransport; use Zend\Mail\Transport\Smtp as SmtpTransport; use Zend\Mail\Transport\SmtpOptions; use Zend\View\Renderer\PhpRenderer; use Zend\View\Resolver\TemplatePathStack; use Zend\View\Model\ViewModel; use Zend\View\Model\ModelInterface; class ApiEmail extends ApiInterface {    protected $From = "XXXXX@gmail.com";    protected $FromName = "XXXX";    protected $To = "" ;    protected $Subject = "" ;    protected $Body = "" ;    protected $Cc = "XXXXXX@gmail.com";    protected $Bcc = "XXXXX@gmail.com" ;    protected $ReplyTo = "XXXX@gmail.com" ;    protected $Sender = "" ;    protected $Encoding = "UTF-8" ;    protected $MESSAGE ; // getter setter    public fun

How to start working with Django web framework

 Django is a powerful web framework for Python that makes it easy to build web applications quickly. Here are some steps you can follow to start learning Django programming: Install Python: Django requires Python to be installed on your machine. If you don ' t already have Python installed, you can download it from the official Python website (https://www.python.org/downloads/). Install Django: Once you have Python installed, you can use the pip package manager to install Django. Open a terminal or command prompt and type the following command: pip install django Create a new Django project: To create a new Django project, open a terminal or command prompt and navigate to the directory where you want to store your project. Then run the following command: django - admin startproject myproject Replace myproject with the name you want to give to your project. This will create a new Django project with the specified name in the current directory. Run the development

How to setup django | django installation ubuntu

Introduction to Django 1.     High level web framework 1.     Basic modules, classes, and tools to quickly develop and deploy web apps 2.     Contains an ORM (Object-Relational Mapper) that allows for the use of standard Python language syntax when interfacing with a back-end database. 1.     Developer need not learn SQL, DDL, etc! 3.     Provides a template framework that allows HTML, XML, and other “documents” to be converted into “templates”, which can then be converted to “output” via a wide range of substitution techniques. 4.     Elegant URL support (fancy, beautiful URL's, no “?blah=blah”, etc.) 5.     Multi-lingual 2.     Fast and easy to use, robust, flexible, and lots of contributed components available! 1.     Built in administrative interface to manage data models. 2.     Built-in authentication/access control components 3.     Contributed Installing & Configuring Django Components 1.     Django can be downloaded from http://w