Search code examples
phpmysqlsymfony1propel

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

Solution

  • 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',
    ));