【问题标题】:Symfony forms: how to set default value for a textarea widgetSymfony 表单:如何为 textarea 小部件设置默认值
【发布时间】:2010-05-02 12:30:16
【问题描述】:

我在 Ubuntu 9.10 上使用 Symfony 1.3.2

我想设置 textarea 小部件的默认值,数据从 adb 读取。

我在模板中的代码 sn-p 如下所示:

<?php $form['notes']->render(); ?>

API 文档没有说明如何执行此操作 - 有人知道如何执行此操作吗?

【问题讨论】:

  • 是的,我看到了那个帖子。我忽略了它,因为它是一个 hack……如果我从 sfForm 而不是基对象继承,我将不得不手动重新实现我的所有小部件和验证器,如果基础模型属性发生变化,这将成为维护的噩梦。

标签: html forms symfony1 textarea


【解决方案1】:

您可以在您的操作或表单类中使用它:

$this->form = new yourForm(); // If its in your action

$text = // data for prepopulating field, from db or anywhere

$this->form->setDefault('notes', $text);

...或者如果您有多个字段:

$this->form->setDefaults(array('notes' => $text, 'more_notes' => $more_text));

或者,如果您更喜欢在表单类配置中使用小部件声明一次(我认为这是正确的):

$this->setWidgets(array(
  // widgets
  'notes' => new sfWidgetFormTextArea(array('default' => $text)),
  // widgets
));

【讨论】:

  • 汤姆:谢谢!做到了(我一直在努力解决这个问题)
  • 应该如何为单个TextareaType 字段做同样的事情?
猜你喜欢
  • 2018-06-29
  • 1970-01-01
  • 2018-03-28
  • 1970-01-01
  • 2020-12-08
  • 2012-01-19
  • 2012-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多