SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Zend Form Questions & Examples

    Im currently working on a site with Zend and im finding that alot of my forms have alot of dropdowns/related data. Currently im a bunch of ->addMultiOptions, etc. in the Controller. This is starting to clutter up the controller with logic that would seem to belong int he Form class itself..

    Does anyone have any examples of Form classes/Controllers that would be willing to throw up - Ill take any examples, im just looking for different ways people do this.

    Here is my current edit action...

    PHP Code:
    public function editAction() {
            
    $_manager = new Manager;

            
    $request $this->getCurrentRequest();

            
    $campaignForm = new Manager_Campaign_Edit;

            
    $campaignForm->advertiser_id->addMultiOptions(array(=> 'None / Default') + Advertiser::getList());

            
    $campaignForm->manager_id->addMultiOptions(array(=> 'None / Master Only') + Manager::getList());

            
    $campaignForm->category_id->addMultiOptions(Category::getList());

            
    $campaignForm->countries->addMultiOptions(Country::getList());

            if(
    $this->_request->isPost()) {
                
    $data $this->_request->getPost();

                
    $campaignForm->redirect_to->addMultiOptions(array(=> 'None') + Campaign::getList(array($data['id'])));

                
    $campaignForm->expire_redirect_to->addMultiOptions(array(=> 'None') + Campaign::getList(array($data['id'])));

                if(
    $campaignForm->isValid($data)) {
                    
    $campaign Doctrine::getTable('Campaign')->find($campaignForm->getValue('id'));

                    
    // Serialize Country Abbreviates
                    
    $countries null;

                    
    $formCountries $campaignForm->getValue('countries');

                    if(!empty(
    $formCountries) && is_array($formCountries)) {
                        
    $countries serialize($formCountries);
                    }

                    
    // Serialize Email From Lines
                    
    $fromLines explode("\n"$campaignForm->getValue('email_from_lines'));

                    if(
    is_array($fromLines)) {
                        
    $fromLines = @serialize($fromLines);
                    }

                    
    // Serialize Email Subject Lines
                    
    $subjectLines explode("\n"$campaignForm->getValue('email_subject_lines'));

                    if(
    is_array($subjectLines)) {
                        
    $subjectLines = @serialize($subjectLines);
                    }

                    
    // Merge the POST and Serialized fields into the existing Record
                    
    $campaign->merge(array_merge($campaignForm->getValues(), array('countries' => $countries'email_from_lines' => $fromLines'email_subject_lines' => $subjectLines)));

                    
    $campaign->save();

                    
    $this->_helper->redirector('index');
                } else {
                    
    $campaignForm->populate($data);
                }
                
                
    $this->view->campaign $_manager->getCampaignById($data['id']);
                
            } else {
                
    $campaign $_manager->getCampaignById($request['params'][0]);
                
                
    $campaignForm->populate($campaign);
                
                
    $this->view->campaign $campaign;
                
                
    $campaignForm->redirect_to->addMultiOptions(array(=> 'None') + Campaign::getList(array($request['params'][0])));

                
    $campaignForm->expire_redirect_to->addMultiOptions(array(=> 'None') + Campaign::getList(array($request['params'][0])));
            }

            
    $this->view->campaignForm $campaignForm;
        } 

  2. #2
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your wouldn't want to put the form in your controller. I create separate files for my forms and place them in a sub folder within my module directory and load it from my model which is helpful whenever I want to supply default values with data from outside sources, like from a database, session or cookie.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  3. #3
    SitePoint Member
    Join Date
    Sep 2008
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All of my forms are in seperate classes/files. The problem is, many of them have alot of dropdowns, etc that need to be populated with the options as well as elements that need validation rules, requirements and filters added or removed depending on other fields in the same form.

    For instance, if someone selects there Country as the United States, I show a dropdown with the list of states and set the state field to be required - else, should the country not be the US, a text region field comes up.

    Im just looking for better ways to handle logic like that and populating default options to elements that doesnt junk up my controllers.

  4. #4
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can always put specific rendering rules in your model or view but I wouldn't put in in the controller.
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are creating dropdowns with static choices like states, I would suggest creating a custom form element by extending Zend_Form_Element_Select. This is what I did for my state, date, time etc dropdowns.

    Here is my State element for example:

    Code PHP:
    <?php
     
    	class CCMS_Form_Element_State extends Zend_Form_Element_Select {
     
    		private $_states = array(
    			'AL'	=>	'Alabama',
    			'AK'	=>	'Alaska', 
    			'AZ'	=>	'Arizona', 
    			'AR'	=>	'Arkansas', 
    			'CA'	=>	'California',
    			'CO'	=>	'Colorado', 
    			'CT'	=>	'Connecticut', 
    			'DC' 	=>	'District of Columbia',
    			'DE'	=>	'Delaware', 
    			'FL'	=>	'Florida', 
    			'GA'	=>	'Georgia', 
    			'HI'	=>	'Hawaii', 
    			'ID'	=>	'Idaho', 
    			'IL'	=>	'Illinois', 
    			'IN'	=>	'Indiana', 
    			'IA'	=>	'Iowa', 
    			'KS'	=>	'Kansas', 
    			'KY'	=>	'Kentucky', 
    			'LA'	=>	'Louisiana', 
    			'ME'	=>	'Maine', 
    			'MD'	=>	'Maryland', 
    			'MA'	=>	'Massachusetts',
    			'MI'	=>	'Michigan', 
    			'MN'	=>	'Minnesota', 
    			'MS'	=>	'Mississippi', 
    			'MO'	=>	'Missouri', 
    			'MT'	=>	'Montana', 
    			'NE'	=>	'Nebraska', 
    			'NV'	=>	'Nevada', 
    			'NH'	=>	'New Hampshire', 
    			'NJ'	=>	'New Jersey', 
    			'NM'	=>	'New Mexico', 
    			'NY'	=>	'New York',
    			'NC'	=>	'North Carolina', 
    			'ND'	=>	'North Dakota', 
    			'OH'	=>	'Ohio', 
    			'OK'	=>	'Oklahoma', 
    			'OR'	=>	'Oregon', 
    			'PA'	=>	'Pennsylvania', 
    			'RI'	=>	'Rhode Island', 
    			'SC'	=>	'South Carolina', 
    			'SD'	=>	'South Dakota', 
    			'TN'	=>	'Tennessee', 
    			'TX'	=>	'Texas', 
    			'UT'	=>	'Utah', 
    			'VT'	=>	'Vermont', 
    			'VA'	=>	'Virginia', 
    			'WA'	=>	'Washington', 
    			'WV'	=>	'West Virginia', 
    			'WI'	=>	'Wisconsin', 
    			'WY'	=>	'Wyoming' 
    		);
     
    		private $_provinces = array(
    			'AB'	=>	'Alberta', 
    			'BC'	=>	'British Columbia', 
    			'MB'	=>	'Manitoba', 
    			'NB'	=>	'New Brunswick', 
    			'NL'	=>	'Newfoundland', 
    			'NT'	=>	'Northwest Territories', 
    			'NS'	=>	'Nova Scotia', 
    			'NU'	=>	'Nunavut', 
    			'ON'	=>	'Ontario', 
    			'PE'	=>	'Prince Edward Island', 
    			'QC'	=>	'Quebec', 
    			'SK'	=>	'Saskatchewan', 
    			'YT'	=>	'Yukon Terroitories'
    		);
     
    		private $_protectorates = array(
    			'AS'	=>	'American Samoa', 
    			'FM'	=>	'Federated States of Micronesia', 
    			'GU'	=>	'Guam',
    			'MH'	=>	'Marshall Islands', 
    			'MP'	=>	'Mariana Islands', 
    			'PW'	=>	'Palau', 
    			'PR'	=>	'Puerto Rico', 
    			'VI'	=>	'Virgin Islands' 
    		);
     
    		private $_military = array(
    			'AE'	=>	'Armed Forces Europe (AE)',
    			'AA'	=>	'Armed Forces Africa (AA)',
    			'AP'	=>	'Armed Forces Pacific (AP)'
    		);
     
    		public function init(){
     
    			$this->addMultiOptions($this->_states);
     
    		}
     
    		public function showProvinces(){
     
    			$this->addMultiOptions($this->_provinces);
     
    			return $this;
     
    		}
     
    		public function showProtectorates(){
     
    			$this->addMultiOptions($this->_protectorates);
     
    			return $this;
     
    		}
     
    		public function showMilitary(){
     
    			$this->addMultiOptions($this->_military);
     
    			return $this;
     
    		}
     
    	}
     
    ?>

    You can always add custom methods to the class to add or remove extra elements if needed. Is this what you are looking for?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •