开发者

Showing default widget value only when object value is not set (in NEW mode)

开发者 https://www.devze.com 2023-01-06 21:57 出处:网络
THIS QUESTION IS NOT ABOUT HOW TO SET DEFAULT VALUE OF A WIDGET Hello Symfonians! I had a fundamental doubt about forms, Im putting 2 scenarios below.
THIS QUESTION IS NOT ABOUT HOW TO SET DEFAULT VALUE OF A WIDGET

Hello Symfonians! I had a fundamental doubt about forms, Im putting 2 scenarios below.

I have a customModelForm that extends a modelForm. 1> If I do not specify a default value for a form field new: field is empty edit: field shows the value in the object 2> If I specify a default value for a field, new: field shows default value edit: field shows default value

I am trying to avoid the EDIT mode behaviour in scenario 2. The default value should only be displayed when the value in the object 开发者_Go百科is not set.

I am calling parent::configure after setting the default value. Do we have any control on the 'bind' event?

Thanks


This shouldn't be happening, at least in Doctrine. The part of the code where this is happening is in updateDefaultsFromObject in sfFormDoctrine. The relevant lines are:

if ($this->isNew())
{
  $defaults = $defaults + $this->getObject()->toArray(false);
}
else
{
  $defaults = $this->getObject()->toArray(false) + $defaults;
}

updateDefaultsFromObject does net get called until the entire configure chain is done, so something else must be going on here.

Are you using Doctrine? Are you using the most current version of Symfony (there was a bug here a while ago)? Are you sure the default is getting set in the configure method of your form?

The isNew check richsage is recommending should be avoided. There is a larger issue here as the proper behavior is for default value to get overwritten by an existing object's values.


First of all, call parent::configure() first in your configure() method. That way you don't run the risk of your configuration being overwritten by the parent configuration.

You can set defaults based on the model's status by doing something like the following in your configure() method:

if ($this->getObject()->isNew())
{
  // do something here but only if the object is new
}
else
{
  // the object is being edited
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号