Musings of ErisDS
ErisDS

I often forget how to set default values for form fields. Mainly because it’s a function of sfForm rather than sfFormField I think. In the snippet below ‘field’ you are setting the default value for, and $value should be the default value.

1
2
3
4
5
6
  <?php
    // lib/form/MyModelForm.class.php
    public function configure() {
      $this->setDefault('field', $value);
    }
?>

A Note on Snippets: When using frameworks such as Symfony it is often the simplest pieces of code which are the hardest to either find or remember. These snippets are placed here for my own reference and will hopefully be useful to others. If you find them useful or have any suggestions, please let me know.

Related Posts

Share this...

  •  Add 'Snippet: Symfony Forms - Setting Default Values' to Del.icio.us
  • Add 'Snippet: Symfony Forms - Setting Default Values' to Twitter
  • Add 'Snippet: Symfony Forms - Setting Default Values' to digg
  • Add 'Snippet: Symfony Forms - Setting Default Values' to FURL
  • Add 'Snippet: Symfony Forms - Setting Default Values' to reddit
  • Add 'Snippet: Symfony Forms - Setting Default Values' to Technorati
  • Add 'Snippet: Symfony Forms - Setting Default Values' to Newsvine
  • Add 'Snippet: Symfony Forms - Setting Default Values' to Stumble Upon
  • Add 'Snippet: Symfony Forms - Setting Default Values' to Google Bookmarks
  • Add 'Snippet: Symfony Forms - Setting Default Values' to FaceBook

Comments

5 Comments to "Snippet: Symfony Forms – Setting Default Values"
  1. 4th Jun

    Joe says:

    Thank you – just what I needed

  2. 8th Jun

    ErisDS says:

    You’re very welcome :)

  3. 7th Aug

    Dratir says:

    Another possibility is to set the default value in the schema. However, I think its prefferable to set it in the Form rather than changing the Database every time ;)

  4. 30th Oct

    Pedro Casado says:

    Cool! I like snippets like these ;D

  5. 17th Dec

    atze says:

    it’s been a while, working with symfony – started digging in the widget-codes for default value etc…
    found your little snippet – voilá

    thx alot, merry chistmas & happy new year

Add your thoughts

  • XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>