Symfony 表单:如何更改表单生成的默认小部件 [英] Symfony forms: how to change default widget for form generation
问题描述
我正在为日期字段使用自定义小部件,并且我想在我的所有表单中使用它.问题是 symfony 使用默认的 sfWidgetFormDate.我想要的是更改此默认小部件,以便使用我的自定义小部件生成表单.我不想手动更改生成的所有表单.
I'm using a custom Widget for date fields, and I want to use it in all my forms. The problem is that symfony uses the default sfWidgetFormDate. What I want is to change this default widget in order to generate forms with my custom Widget. I don't want to change by hand all the forms generated.
我发现的唯一方法是修改 BaseFormDoctrine.php 的技巧:
The only approach I have found is the trik of modify 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',
));
}
}
}
推荐答案
您可以做的是创建自己的表单生成器类.
What you could do is create your own form generator class.
class myFormGenerator extends sfDoctrineGenerator
{
public function getWidgetClassForColumn($column)
{
switch ($column->getDoctrineType())
{
case 'date':
return 'sfWidgetFormJQueryDate';
break;
default:
return parent::getWidgetClassForColumn($column);
}
}
}
将其保存在 lib 文件夹中的某个位置,清除缓存等.
Save that somewhere sensible in your lib folder, clearing the cache etc..
然后像这样重新运行你的 for 生成器......
Then rerun your for generator like so...
php doctrine:build-forms --generator-class='myFormGenerator'
我没有尝试过以上任何一种,但我认为这个理论是合理的......
I haven't tried any of the above, but the theory is sound I think...
查看以下文件,看看我是从哪里想到的:
Have a look at the following files to see where I figured this out from:
lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildFormsTask.class.php
lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormGenerator.class.php
这篇关于Symfony 表单:如何更改表单生成的默认小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!