【发布时间】:2010-05-24 17:48:08
【问题描述】:
我正在使用一些自定义表单,其中一个没有将其值绑定到 POST。我对每个都使用相同的逻辑,但只有一个不起作用。代码如下:
public function executeMediaFileUpload(sfWebRequest $request) {
$this->form = new MediaFileUploadForm();
if (!$request->isMethod('POST'))
$psk = $request->getParameter('psk');
else {
$this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
$this->logMessage('VALUE: ' . $this->form->getValue('version'));
$psk = $this->form->getValue('application');
}
$this->logMessage('PSK: ' . $psk);
$app = Doctrine::getTable('Application')->find(array($psk));
$this->form->setDefault('application', $app->getPsk());
$this->form->setDefault('version', $app->getVersion()->getLast()->getPsk());
在初始 GET 中,我可以看到通过 psk 传入的值在生成的 HTML 中被设置为应用程序的默认值,并且所有值都显示在 Firebug 的 POST 请求中,但是在我绑定表单后,它仍然不包含任何值。
编辑: 是的,表单设置为多部分和 POST。我在表单中调用 $this->widgetSchema->setNameFormat('values[%s]') 。
EDIT2:这是表单的 HTML,getName 返回“值”:
<form name="mediafiles" action="<?php echo url_for('application/mediaFileUpload') ?>" method="POST" <?php $form->isMultipart() and print 'enctype="multipart/form-data" ' ?> id="applications">
<div class="clear">
<div>
<?php echo $form->renderHiddenFields() ?>
<?php foreach($form as $widget): ?>
<?php if (!$widget->isHidden()): ?>
<?php echo $widget->renderLabel() ?>
<?php echo $widget->renderError() ?>
<?php echo $widget->render() ?>
<?php endif ?>
<?php endforeach ?>
</div>
</div>
<!-- Start Bottom Sub Navigation -->
<div class="subnav clear">
<a href="<?php echo url_for('application/index') ?>">Cancel</a>
<a href="<?php echo url_for('application/index') ?>" onclick="document.forms['mediafiles'].submit(); return false;">Upload</a>
</div>
<!-- End Bottom Sub Navigation -->
【问题讨论】:
-
请求对象中存储的值是否在正确的数组中才能使用
$this->form->getName()? -
您的 HTML 表单是否使用:method="post" enctype="multipart/form-data"?
-
$request->getParameter($this->form->getName()) 的值是多少?代码对我来说看起来不错,所以我会指出表单的 html 不正确。
-
嗯,看起来也不错。很奇怪。认为我们也需要查看表单类!