Symfony Forms: How to Change the Default Widget to Form

I am using a custom widget for date fields, and I want to use it in all my forms. The problem is that symfony uses the standard sfWidgetFormDate. I want to change this default widget to create forms using my custom widget. I do not want to manually change all created forms.

The only approach I found is a trik to 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', )); } } } 
+4
source share
2 answers

What you can do is create your own form generator.

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

Save this somewhere reasonable in your lib folder by flushing the cache, etc.

Then run your generator like this ...

 php doctrine:build-forms --generator-class='myFormGenerator' 

I have not tried any of the above, but the theory sounds, I think ...

Take a look at the following files to find out how I got this:

 lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBuildFormsTask.class.php lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/generator/sfDoctrineFormGenerator.class.php 
+5
source

After johnwards answer, since I want to define default options for the widget, I also override the function for this:

 class myFormGenerator extents sfDoctrineFormGenerator { public function getWidgetClassForColumn($column) { ... } public function getWidgetOptionsForColumn($column) { switch ($column->getDoctrineType()) { case 'date': return "array('config' => '{}', 'image'=>'/images/calendar.png')"; break; default: return parent::getWidgetOptionsForColumn($column); } } } 
0
source

Source: https://habr.com/ru/post/1310481/


All Articles