Symfony form->bind fails
I'm working with a few custom forms, and one of them isn't binding it's values on a POST. I'm using the same logic for each, and only one of them isn't working. Here's the code:
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->for开发者_如何学Gom->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());
On the initial GET, I can see the value passed in via psk getting set as the default for application in the generated HTML, and all the values show up in the POST request in Firebug, but after I bind the form, it still contains no values.
EDIT: Yes, the form is setup as multipart and POST. And I'm calling $this->widgetSchema->setNameFormat('values[%s]') in the form.
EDIT2: Here's the HTML for the form, and getName returns "values":
<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 -->
Check in the code generated on your browser (or firebug) for the form. Form values use to have a name, according to your "name format" configuration, like this:
<input type="text" name="my_name_format[widget_name]" />
So in order to access its value passed to action you must use something like this:
$parameters = $request->getParameter('my_name_format');
$parameter = $parameters['widget_name'];
And like this for each value of your form using "parameters" array.
An simple draft implementing my solution into your code looks like this:
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());
Hope this solution works.
精彩评论