How does one set the default form value of a Symfony form embedded within another form that are both associated with entities?
如何设置嵌入在另一个表单中的Symfony表单的默认表单值,这些表单都与实体相关联?
If I try to set the street property in my PropertyLocation entity to a default value in the following example, this default value does not get shown when the form renders. I know there is a data option for each form field that I could use, but I would rather not do it this way since that overrides what is set in the entity. How can I make the form show the default value stored in the entity.
如果我尝试在我的PropertyLocation实体中将street属性设置为以下示例中的默认值,则在表单呈现时不会显示此默认值。我知道我可以使用每个表单字段的数据选项,但我不想这样做,因为它会覆盖实体中设置的内容。如何使表单显示存储在实体中的默认值。
class PropertyType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('propertyLocation', new PropertyLocationType());
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(
array('data_class' => 'UR\AppBundle\Entity\Property'
));
}
/**
* @return string
*/
public function getName()
{
return 'property';
}
}
Property Location Type looks like:
物业位置类型看起来像:
class PropertyLocationType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('street', 'text');
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'UR\AppBundle\Entity\PropertyLocation'
));
}
/**
* @return string
*/
public function getName()
{
return 'propertyLocation';
}
}
1 个解决方案
#1
0
Default values could be set in the newAction
within your controller :
可以在控制器的newAction中设置默认值:
Let's say that you have a PropertyController.php as a controller for your Property entity
假设您有一个PropertyController.php作为Property实体的控制器
Your newAction
will look like this :
你的newAction将如下所示:
public function newAction()
{
$defaultPropertyLocation = new PropertyLocation();
$defaultPropertyLocation->setStreet('default value ');
$property = new Property();
$property->setPropertyLocation($defaultPropertyLocation);
// now you could pass your property entity to get rendred
$form = $this->createCreateForm($property);
return $this->render('YourBundle:Property:new.html.twig', array(
'entity' => $property,
'form' => $form->createView(),
));
}
Edit Second option : use data
field
编辑第二个选项:使用数据字段
->add('myfield', 'text', array(
'label' => 'Field',
'data' => 'Default value'
))
AFAIK There is no third option.
AFAIK没有第三种选择。
#1
0
Default values could be set in the newAction
within your controller :
可以在控制器的newAction中设置默认值:
Let's say that you have a PropertyController.php as a controller for your Property entity
假设您有一个PropertyController.php作为Property实体的控制器
Your newAction
will look like this :
你的newAction将如下所示:
public function newAction()
{
$defaultPropertyLocation = new PropertyLocation();
$defaultPropertyLocation->setStreet('default value ');
$property = new Property();
$property->setPropertyLocation($defaultPropertyLocation);
// now you could pass your property entity to get rendred
$form = $this->createCreateForm($property);
return $this->render('YourBundle:Property:new.html.twig', array(
'entity' => $property,
'form' => $form->createView(),
));
}
Edit Second option : use data
field
编辑第二个选项:使用数据字段
->add('myfield', 'text', array(
'label' => 'Field',
'data' => 'Default value'
))
AFAIK There is no third option.
AFAIK没有第三种选择。