Search code examples
phpsymfony1symfony-1.4symfony-forms

Symfony - set form value on save


A quick question. I have a symfony form. When I save the form, I want my created_by field to be set automatically on save.

So, I'm passing the current user id as an option in the form:

$this->form = new ContractForm(null,array("created_by"=>$this->getUser()->getId()));

And in the configure method of the form class I have:

$this->setDefault('created_by', $this->getOption("created_by"));

If I had a created_by widget and set it to hidden, this would work great, however, I don't want to have a field displayed as a user could easily manipulate using firebug or other tools alike.

So my question to you, how do I save a column value if the field does not exist as a widget?


Solution

  • You dont need to have a widget for this, just in the configure method of your form call.

    <?php
    $this->getObject()->setCreatedBy($this->getOption('created_by'));
    ?>
    

    There really is no need to overwrite a method.

    Also for this to work youd have to initiate your form like this

    <?php
    $this->form = new ContractForm(new Contract(), array('created_by' => $this->getUser()->getId()));`
    ?>