Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In symfony, how to set the value of a form field?

I'm overriding my doSave() method to basically do the following: I have a sfWidgetFormPropelChoice field that the user can either choose from, or type a new option. How can I change the widget's value? Or maybe I am approaching this the wrong way. So here is how I overrode the doSave() method:

public function doSave($con = null)
{
    // Save the manufacturer as either new or existing.
    $manufacturer_obj = ManufacturerPeer::retrieveByName($this['manufacturer_id']->getValue());
    if (!empty($manufacturer_obj))
    {
        $this->getObject()->setManufacturerId($manufacturer_obj->getId()); // NEED TO CHANGE THIS TO UPDATE WIDGET'S VALUE INSTEAD?
    }
    else
    {
        $new = new Manufacturer();
        $new->setName($this['manufacturer_id']->getValue());
        $new->save();
        $this->getObject()->setManufacturerId($new->getId()); // NEED TO CHANGE THIS TO UPDATE WIDGET'S VALUE INSTEAD?
    }

    parent::doSave($con);
}
like image 347
James Skidmore Avatar asked Jun 22 '09 22:06

James Skidmore


1 Answers

You should use setDefault or setDefaults and then it will autopopulate with the bound values.

(sfForm) setDefault ($name, $default)
(sfForm) setDefaults ($defaults)

usage

$form->setDefault('WidgetName', 'Value');
$form->setDefaults(array(
    'WidgetName' => 'Value',
));
like image 112
Henrik Bjørnskov Avatar answered Oct 12 '22 11:10

Henrik Bjørnskov