2013-09-24 62 views
0

我有一個關於在twitter引導模式中使用zend表單的問題。這是我有:Zend Form in Modal - 提交表格

在我的控制器:

public function accountAction() { 

    $form = new Form_CancelAccount(); 
    $this->view->form = $form; 

    // Some executions and view params for the view, ex: 
    /*if ($user->getRecurlyId() && NameSpace_Feature_Access_RoleHelper::hasAccess('show_billing')) { 
     try { 
      $account = Recurly_Account::get($user->getRecurlyId()); 
      $this->view->token = $account->hosted_login_token; 
      $this->view->show_billing = true; 
     } catch (Recurly_NotFoundError $e) { 
      $this->view->show_billing = false; 
     } 
    } else { 
     $this->view->show_billing = false; 
    }*/ 

    if ($this->getRequest()->isPost() && $form->isValid($_POST)) { 

     $data = $form->getValues(); 
     var_dump($data); 

    } else { 
     var_dump("it didn't work!"); 
    } 
} 

我CancelAccount形式:

<?php 

class Form_CancelAccount extends NameSpace_Form_Base 
{ 
    public function __construct($options = null) { 

    parent::__construct($options); 

    $this->setName('cancelaccount'); 

    $element = new Zend_Form_Element_Radio('reason'); 
    $element->addMultiOptions(array(
     'It is really bad!' => 'bad', 
     'No time for it!' => 'notime', 
     'Other option:' => 'otheroption' 
    ))->setDecorators(array(array('ViewScript', array('viewScript' => 'user/radio.phtml')))); 

    $this->addElement($element); 

    $this->addElement('text', 'otheroption', array(
     'attribs' => array(
      'class' => 'input-block-level otheroption', 
      'size' => 30, 
      'placeholder' => 'Other option' 
     ), 
     'decorators' => $this->elementDecoratorsNoTag 
    )); 
    } 
} 

在我看來:

// Some data sent from the controller 
<h4>Delete account</h4> 
<p>Deleting of your account implies your account and the related data will be deleted. Please note that this is not reversible.</p> 

<button type="button" data-target="#cancelModal" data-toggle="modal" class="btn btn-primary">Delete account</button> 
<div class="modal hide fade" id="cancelModal"> 
<div class="modal-header"> 
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">x</button> 
    <h3>Modal header</h3> 
</div> 
<div class="modal-body"> 
    <?php echo $this->form; ?> 
</div> 
<div class="modal-footer"> 
    <input type="submit" class="btn btn-default" value="OK" form="cancelaccount"> 
    <a id="cancel" class="btn btn-default close" data-dismiss="modal">Cancel</a> 
</div> 

但我始終得到輸出'it didn't work!'來自var_dump當窗體無效...。什麼是最好的方法來做到這一點?它是如何發生的,它總是無效的?

UPDATE:
當我離開了"&& $form->isValid($_POST)"在我的if語句我進來的語句,但我的var_dump表明這一點:

陣列(大小= 2)
'理由'= >空
'otheroption'=>空

我的響應: enter image description here

問題是我的單選按鈕無效......我該如何解決這個問題?

+0

你確定窗體使用'POST'方法嗎? – Mina

+0

是的,當我離開「&& $ form-> isValid($ _ POST)」時,如果我收到迴應。只有字段是空的..(更新開始發佈) – nielsv

回答

0

的問題,在我的單選按鈕。將我的表單中的單選按鈕更改爲:

$this->addElement('radio', 'reason', array(
     'label'=>'Reason:', 
     'multiOptions'=>array(
      'bad' => 'It is really bad!', 
      'notime' => 'No time for it!', 
      'otheroption' => 'Other option:' 
     ), 
    )); 

現在可行!

0
if ($this->getRequest()->isPost() && $form->isValid($_POST)) { 

    $data = $form->getValues(); 
    var_dump($data); 

} else { 
    var_dump("it didn't work!"); 
} 

if ($this->getRequest()->isPost()) { 
    if($form->isValid($this->getRequest()->getPost()){ 
    $data = $form->getValues(); 
    var_dump($data); 

    } else { 
     var_dump("it didn't work!"); 
    } 
} 

,並記得把這個動作的最後一行:

$this->view->form = $form; 

對不起,我講英語不好。

enter image description here

+0

我仍然得到var dump'它沒有工作!' – nielsv

+0

問題是我的單選按鈕無效,我該如何解決這個問題。 – nielsv

+0

我測試了你的代碼,它的工作,我看到POST數據是正確的調試環境(螢火蟲)和無線電價值在那裏。 只看到我的文章中的圖片。 您可能需要進行一些驗證才能形成輸入。 –