1
votes

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->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());

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 -->
1
Are the values stored in the request object in the correct array to be able to use $this->form->getName()? - richsage
Does your HTML form use: method="post" enctype="multipart/form-data"? - Tom
What is the value of $request->getParameter($this->form->getName())? The code looks fine to me so I would point my finger at the html of the form not being correct. - johnwards
Hmm that all looks fine too. Very strange. Think we are going to need to see the form class too! - johnwards

1 Answers

2
votes

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.