2014-03-19 102 views
1

我得到了一個用戶註冊的表單,並將其用作編輯用戶表單。 現在我想讓這個表單更可變。這意味着我的情況:我想爲不同的動作有一個特定的按鈕標籤。
如果通過RegisterAction調用表單,則標籤應爲「註冊」,如果通過EditAction調用,則應爲「更新用戶」。我嘗試了一些東西,但現在我用完了想法。可變按鈕標籤

這裏是我的代碼:

CustomerController.php

... 
public function registerAction(){ 
     $form = new Application_Form_Register(); 
     $request = $this->_request->getParams(); 

    if(isset($request['registerbtn']) && ($form->isValid($request))){ 
     $customerModel = new Application_Model_Customer(); 
     $customerArr = $customerModel->setCustomer($request,true); 
     $this->redirect('/customer/detail/id/'.$customerArr); 
    } 
    else{ 
     $this->view->form = $form; 
     $this->view->button = "Register"; //TEST 
    } 
} 

public function editAction(){ 
     $request = $this->_request->getParams(); 
     if(isset($request['id']) && !empty($request['id'])){ 
      $form = new Application_Form_Register(); 

      $form->addElement('hidden', 'id', array(
       'required' => true, 
       'value'  => $request['id'], 
       'validators' => array(
        $digits = new Zend_Validate_Digits() 
       ) 
      )); 

      if(isset($request['registerbtn']) && ($form->isValid($request))){ 
       $customerModel = new Application_Model_Customer(); 
       $id = $customerModel->setCustomer($request,false); 
       $this->redirect('/customer/detail/id/'.$id); 
      }else{ 
       $modelResult = new Application_Model_Customer(); 
       $customer = $modelResult->getCustomer($request['id']); 
       $cArr = $customer->toArray(); 
       $form->populate($cArr); 

       $this->view->form = $form; 
       $this->view->button = "Update user"; //TEST 
      } 
     }else{ 
      $this->redirect('/'); 
     } 
} 
... 

意見

// register.phtml - begin 
<h2>Registration</h2> 
<?php 
    $this->headTitle('Registration'); 

    $button = $this->button; //TEST 
    $this->form->button = $button; //TEST 

    echo $this->form; 
    echo $this->error;?> 
// register.phtml - end 

// edit.phtml - begin 
<?php 
    echo $this->headline; 
    $this->headTitle('Update user'); 

    $button = $this->button; //TEST 
    $this->form->button = $button; //TEST 

    echo $this->form; 
?> 
// edit.phtml - end 

和表單

// 
    ... 
    $this->addElement('submit', 'registerbtn', array(
     'ignore' => true, 
     'label' => $button, //TEST 
     'decorators' => $this->buttonDecorators, 
    )); 
    ... 

我擔心這是完全錯誤的,但我不知道如何做對。

回答

0

試着這麼做

​​
+0

我試過 「$形式 - > getElement( 'registerbtn') - > setLabel( '註冊');」它完美的作品。非常感謝你。 =) – Bazti

+0

:)不客氣,祝你好運! – doydoy44