2013-01-11 51 views
0

我有一個Zend表單和一個到這個表單的URL。Zend表單和URL參數

該URL有3個參數firstname,lastname和email。 在我的表格中,我有3個輸入字段,也有名字,姓氏和電子郵件。

每當我提交表格。它從URL參數中獲取值而不是表單輸入字段。

有沒有辦法在窗體中獲取值?

我知道,從URL中的參數具有相同的名稱,表單輸入字段*

網址:http://mywebsite.com/thank-you?key=e72671a8640b45c2401afe887b9b530a&FIRST_NAME =首先& 姓氏 =用戶& 電子郵件 [email protected]

Zend動作控制器的形式:

$signupForm = new Application_Form_UserSignUp(); 
if ($signupForm->isValid($this->getRequest()->getParams())) 
     { 

      $user = $this->_helper->model('Users')->createRow($signupForm->getValues()); 
      if ($user->save()) 
      { 
       Zend_Session::rememberMe(186400 * 14); 
       Zend_Auth::getInstance()->getStorage()->write($user); 
       $user->sendSignUpEmail(); 
       $this->getHelper('redirector')->gotoRoute(array(), 'invite'); 
       return; 
      } 
     } 
$this->view->signupForm = $signupForm; 

Zend的形式:

class Application_Form_UserSignUp extends Zend_Form 
{ 
public $first_name, $last_name, $email, $submitButton; 

public function init() 
{ 
    $this->setName('signupForm'); 

    $this->first_name = $this->createElement('text', 'first_name')->setRequired(true); 
    $this->last_name = $this->createElement('text', 'last_name')->setRequired(true); 

    // Check if email is duplicated in database 
    $noEmailExists = new Zend_Validate_Db_NoRecordExists(
      array(
       'table' => 'users', 
       'field' => 'email' 
      ) 
     ); 
    $noEmailExists->setMessage('%value% is already used by another person, please try again with different email address', Zend_Validate_Db_Abstract::ERROR_RECORD_FOUND); 

    $this->email = $this->createElement('text', 'email') 
        ->setLabel('Email') 
        ->addValidator($noEmailExists) 
        ->addValidator('EmailAddress') 
        ->setRequired(true); 

    $this->submitButton = $this->createElement('button', 'save') 
          ->setLabel('Sign Up') 
          ->setAttrib('type', 'submit'); 

    $this->addElements(array($this->first_name, $this->last_name, $this->email, $this->submitButton)); 

    $elementDecorators = array(
     'ViewHelper' 
    ); 
    $this->setElementDecorators($elementDecorators); 

} 

} 

回答

1

在你的控制器動作,你可以通過值Zend_Form的對象:

$form->populate($data); 

OR $形式 - > setDefaults($的數據);

其中populate()接受一個數組,其中鍵是表單字段的名稱,數組值是字段值。

http://framework.zend.com/manual/1.12/en/zend.form.forms.html#zend.form.forms.elements.values

+0

我認爲我解決了它。代替$ signupForm-> isValid($ this-> getRequest() - > getParams()),我改變了$ signupForm-> isValid($ this-> getRequest() - > getPost()) –

+0

是的,需要POST變量,但是如果您希望在提交之前填充填充值的表單,則必須使用填充。 –