来自扩展类的 Symfony 表单默认值


Symfony form default value from extended class

我正在尝试为 symfony 中的表单设置默认值,但它似乎不起作用。窗体映射到实体。

我知道如果您在实体中设置值,那么这将是默认值,但就我而言,实体扩展了另一个类(映射的超类),并且我的字段在该类上。

/** @ORM'MappedSuperclass */
abstract class BaseEntity implements CustomEntityInterface
{
  protected $choiceField = 30;
 [ getters, setters, ... ]
}
class MyEntity extends BaseEntity
{
  [other attributes, getters, setters, ...]
}

选择字段应默认为 30,但事实并非如此。(这是一个表示分钟的选择字段,填充了从 5 到 60 的值,步长为 5)

我知道我可以在MyEntity中重新声明$this->选择字段,但这对我来说似乎不对。还有其他解决方案吗?

谢谢斯蒂夫

我想你的意思是:

abstract class BaseEntity implements CustomEntityInterface
{
    protected $choiceField = 30;
}

默认值表单Symfony2表单可以在表单创建过程中简单地设置:

public function buildForm(FormBuilderInterface $builder, array $options) 
{
    $builder
        ->add('choiceField', 'text', array(
            'label' => 'Field',
            'data' => 'Default value'
        ))
        // ...
    ;
}

以及设置实体属性值。