Symfony forms: how to change default widget for form generation

后端 未结 2 1642
刺人心
刺人心 2021-02-06 16:23

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 de

2条回答
  •  离开以前
    2021-02-06 17:26

    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); 
        }
      }
    }
    

    Save that somewhere sensible in your lib folder, clearing the cache etc..

    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
    

提交回复
热议问题