This is some stuff for symfony beginners, who still want to learn symfony 1.4. You may set default form values for all kind of forms (including doctrine forms). Set one default value at a time:
class XxxForm extends BaseXxxForm { public function configure() { $this->setDefault ('field', 'value'); } }or set a whole array of them:
public function configure() { $this->setDefaults(array( 'field_1' => 'value_1', 'field_2' => 'value_2', // ... 'field_x' => 'value_x' )); }
default values for new objects
Sometimes you want to set the default form values just before the object is created, because it'd be easier for the aplication user to fill in some data. For example, the owner/author of a blog post may be set default to the current logged in user - or the date of an event may be set to now - and so on. This can be achieved with the isNew method of the doctrine form class (lib/form/doctrine/XxxForm.class.php):
if ($this->isNew()) { $this->setDefault ('created_at'1, date('Y-m-d 00:00:00')); $this->setDefault ('created_by'2, sfContext::getInstance()->getUser()3->getId()); }
- note 1: Timestampable Doctrine behavior used in this example,
- note 2: Signable Doctrine behavior used in this example,
- note 3: check this blog post to avoid using sfContext::getInstance().
moreover
You may implement whatever complex conditions you want your doctrine form to follow. Look at some of examples below:
- current time - php time function
- language/localization (default country when registering a new user) - use Accept-Language HTTP
- default settings set for a registered user - fetch individual user settings from database (doctrine query)
- last used item (category/product/etc.) - a user inserts or updates a large amount of data, when he choses a specific item (category/product) it can be saved in its session ($user->set/getAttribute()) - when another record is processed, last used item is used as default (which, again, lowers time needed for the user to work)
It's too bad the default parameter in schema.yml is ignored when doing model forms.
ReplyDeletesymfony is great
ReplyDelete