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.












4th Jun
Joe says:
Thank you – just what I needed
8th Jun
ErisDS says:
You’re very welcome :)
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 ;)
30th Oct
Pedro Casado says:
Cool! I like snippets like these ;D
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