我有一个关于在twitter bootstrap模式中使用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!");
}
}
我的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>
但是当表单无效时,我总是从var_dump得到输出'it didn't work!'
....最好的方法是什么?它是如何来的总是无效的?
更新
当我在if语句中省略"&& $form->isValid($_POST)"
时,我会在声明中出现,但我的var_dump显示了这一点:
数组(大小= 2)
'reason'=&gt;空
'otheroption'=&gt;空
我的回答:
问题是我的无线电按钮无效......我该如何解决这个问题?
答案 0 :(得分: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;
对不起,我说英语不太好。
答案 1 :(得分:0)
我的单选按钮的问题。将表单中的单选按钮更改为:
$this->addElement('radio', 'reason', array(
'label'=>'Reason:',
'multiOptions'=>array(
'bad' => 'It is really bad!',
'notime' => 'No time for it!',
'otheroption' => 'Other option:'
),
));
现在有效!