Php 扩展类中的Symfony表单默认值
我试图在symfony中为表单设置默认值,但它似乎不起作用。表单映射到一个实体 我知道如果在实体中设置值,那么这将是默认值,但在我的例子中,实体扩展了另一个类a映射超类,我的字段位于该类上Php 扩展类中的Symfony表单默认值,php,symfony,Php,Symfony,我试图在symfony中为表单设置默认值,但它似乎不起作用。表单映射到一个实体 我知道如果在实体中设置值,那么这将是默认值,但在我的例子中,实体扩展了另一个类a映射超类,我的字段位于该类上 /** @ORM\MappedSuperclass */ abstract class BaseEntity implements CustomEntityInterface { protected $choiceField = 30; [ getters, setters, ... ] } cla
/** @ORM\MappedSuperclass */
abstract class BaseEntity implements CustomEntityInterface
{
protected $choiceField = 30;
[ getters, setters, ... ]
}
class MyEntity extends BaseEntity
{
[other attributes, getters, setters, ...]
}
choiceField应该默认为30,但它不是。这是一个分钟的选择字段,用5到60的值填充,步长为5
我知道我可以在MyEntity重新申报$this->choiceField,但我觉得这不太合适。还有其他解决办法吗
谢谢,
我想你的意思是:
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'
))
// ...
;
}
还有设置实体属性值。很抱歉,我从IDE粘贴了一个坏副本:,如果我在BaseEntity中设置了该值,它就不会在表单中使用它。如果可以在一个地方设置这个值,我很好奇,最好是在BaseEntity中。