开发者

Different form formatter for embeded form?

开发者 https://www.devze.com 2023-01-28 00:33 出处:网络
I\'m trying to change the form formatter of the embeded form. Is it possible to approach something like this?

I'm trying to change the form formatter of the embeded form. Is it possible to approach something like this?

class sfOuterForm extends sfForm {
  public function configure()
  {
    $innerForm = new sfForm();
    $this->embedForm('inner', $innerForm);
    $this->getWidgetSchema()->setFormFormatter('list');
    $this->getEmbeddedForm('inner')->getWidgetSchema()->setFormFormatterName('table');
  }
}

i'm expecting the following:

echo (new sfOuterForm())

outputs:

<li><label>Outer Label</label><input type="text" /></li>
<li>
  <table>
   <tr><td><label>Inner Label</label></t开发者_如何学Pythond><td><input type="text" /></td></tr>
  </table>
</li>


Once a form is embedded, it's original widget schema and validator schema do nothing - they've been merged into the top level schemas. Thus, you need to set the form formatter before embedding:

$this->getWidgetSchema()->setFormFormatter('list');
$innerForm = new sfForm();
$innerForm->getWidgetSchema()->setFormFormatterName('table');
$this->embedForm('inner', $innerForm);

It's worth a look into sfForm::embedForm to see what's going on internally.


I'll answer my question by myself :) The problem arised when i tried to change formatter for relation's embedded forms. I solved this as follows:

class sfOuterForm extends sfForm {
  public function configure()
  {
    $innerForm = new sfForm();
    $this->embedRelation('relationName');
    $this->getWidgetSchema()->setFormFormatter('list');
    $this->getEmbeddedForm('relationName')->getWidgetSchema()->setDefaultFormFormatterName('table');
  }
}

Hope this will help someone :)

0

精彩评论

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

关注公众号