0

我有一个关于在 twitter 引导模式中使用 zend 表单的问题。这就是我所拥有的:

在我的控制器中:

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!");
    }
}

我的取消帐户表格:

<?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 获得输出...。最好的方法是什么?它是怎么来的,它总是无效的?

更新:
当我"&& $form->isValid($_POST)"在 if 语句中省略时,我进入了语句,但我的 var_dump 显示了这一点:

数组 (size=2)
'原因' => null
'其他选项' => null

我的回复: 在此处输入图像描述

问题是我的单选按钮无效......我该如何解决这个问题?

4

2 回答 2

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;

对不起,我英语说得不好。

在此处输入图像描述

于 2013-09-24T14:54:34.343 回答
0

我的单选按钮的问题。将我表单中的单选按钮更改为:

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

现在它起作用了!

于 2013-09-24T15:11:14.950 回答