生成的symfony模块中用于编辑或创建对象的不同小部件


Different widgets in generated symfony module for editing or creating an object

在下面的generator.yml中,我可以更改后台使用的表单的类。但是,我希望在编辑现有对象或创建新对象时使用不同的形式。

编辑对象时,我希望取消设置字段"开始",而不会在创建新对象时丢失它。这可能吗?如果可能,怎么做?

JobeetCategory:
  columns:
    name: { type: string(255), notnull: true, unique: true }
    test: { type: string(255) }
    onset:  { type: boolean }

generator.yml:
config:
  actions: ~
  fields:  ~
  list:
    title: Category Management
  filter:  ~
  form:    
      class: TestForm
  edit:
    title: Editing Category "%%name%%"
  new:
    title: New Category

您可以有条件地在表单类中设置或取消设置小部件,这取决于您的对象是新的还是正在编辑:

<?php
class JobeetCategoryForm extends BaseJobeetCategoryForm {
  public function configure() {
    $this->setWidgets(array(
      'onset' => new sfWidgetFormInputCheckbox()
      // other widgets...
    ));
    $this->setValidators(array(
      'onset' => new sfValidatorBoolean()
      // other validators...
    ));
    if (!$this->object->isNew()) {
      // we are editing an existing category
      unset($this['onset']);
    }
    // ...
  }
}