【问题标题】:Zend Form in Modal - Submit FormZend Form in Modal - 提交表单
【发布时间】:2013-09-24 12:55:20
【问题描述】:

我有一个关于在 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>

但是当表单无效时,我总是从 var_dump 获得输出 'it didn't work!' ... 。最好的方法是什么?它是怎么来的,总是无效的?

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

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

我的回答:

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

【问题讨论】:

  • 您确定表单使用的是POST 方法吗?
  • 是的,如果我收到回复,我会离开“&& $form->isValid($_POST)”。只有字段是空的..(更新开始帖子)

标签: javascript php zend-framework submit zend-form


【解决方案1】:

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

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

现在可以了!

【讨论】:

    【解决方案2】:
    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;
    

    对不起,我的英语不太好。

    【讨论】:

    • 问题是我的单选按钮无效,我该如何解决这个问题。
    • 我已经测试了你的代码,它的工作,我看到 POST 数据在调试环境(萤火虫)中是正确的,并且无线电值在那里。只需在我的帖子中查看图像。您可能需要对表单输入进行一些验证。
    猜你喜欢
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2018-05-28
    • 2015-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多