【问题标题】:Symfony form->bind failsSymfony 表单->绑定失败
【发布时间】: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-&gt;form-&gt;getName()
  • 您的 HTML 表单是否使用:method="post" enctype="multipart/form-data"?
  • $request->getParameter($this->form->getName()) 的值是多少?代码对我来说看起来不错,所以我会指出表单的 html 不正确。
  • 嗯,看起来也不错。很奇怪。认为我们也需要查看表单类!

标签: php symfony1


【解决方案1】:

签入在您的浏览器(或萤火虫)上为表单生成的代码。根据您的“名称格式”配置,表单值使用名称,如下所示:

<input type="text" name="my_name_format[widget_name]" />

因此,为了访问其传递给操作的值,您必须使用如下内容:

$parameters = $request->getParameter('my_name_format');
$parameter = $parameters['widget_name'];

像这样使用“参数”数组对表单的每个值进行处理。


在您的代码中实施我的解决方案的简单草稿如下所示:

public function executeMediaFileUpload(sfWebRequest $request) {
$this->form = new MediaFileUploadForm();
$parameters = $request->getParameter('values'); // Since you used $this->widgetSchema->setNameFormat('values[%s]')

if (!$request->isMethod('POST')) 
    $psk = $parameters['psk'];
else {
    $this->form->bind($request->getParameter($this->form->getName()), $request->getFiles($this->form->getName()));
    $this->logMessage('VALUE: ' . $parameters['version']);
    $psk = $parameters['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());

希望这个解决方案有效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多