symfony1 - Symfony 表单 : how to change default widget for form generation

标签 symfony1 widget

我正在为日期字段使用自定义小部件,并且我想在我的所有表单中使用它。问题是 symfony 使用默认的 sfWidgetFormDate。我想要的是更改此默认小部件,以便使用我的自定义小部件生成表单。我不想手动更改生成的所有表单。

我发现的唯一方法是修改 BaseFormDoctrine.php 的技巧:

public function setup()
{
    foreach($this->getWidgetSchema()->getFields() as $name=>$widget)
    {
        if($widget instanceof sfWidgetFormDate) 
        {
            $this->widgetSchema[$name] = new sfWidgetFormJQueryDate(array(
                'config' => '{}',
                'image'=>'/images/calendar.png',
            ));
        }
    }
}

最佳答案

您可以做的是创建自己的表单生成器类。

class myFormGenerator extends sfDoctrineGenerator
{

  public function getWidgetClassForColumn($column)
  { 
    switch ($column->getDoctrineType())
    {
      case 'date':
        return 'sfWidgetFormJQueryDate';
        break;
      default:
        return parent::getWidgetClassForColumn($column); 
    }
  }
}

将其保存在 lib 文件夹中的某个位置,清除缓存等。

然后像这样重新运行你的 for 生成器......
php doctrine:build-forms --generator-class='myFormGenerator'

我没有尝试过以上任何一种,但我认为这个理论是合理的......

查看以下文件,看看我是从哪里想到的:
lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildFormsTask.class.php
lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormGenerator.class.php

关于symfony1 - Symfony 表单 : how to change default widget for form generation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2881379/

相关文章:

python-3.x - 为什么网格只在最后一个子图上打开?

mysql - 复制具有关系的数据库记录

php - Doctrine_Table 中的查询生成器?

iOS 8 beta - Today 扩展无法识别嵌入式框架

Flutter:Widget State:这段代码安全吗?

python - matplotlib.widgets.TextBox : change color

php - 保存嵌入 sfForm

php - 推进和左加入

php - 交响乐团 : REST web-service for bots and humans - open questions

c++ - 如何在 Qt Widgets 中删除带有插槽的按钮