[英]How do I display data for an embedded object in an embedded form using Silex and Symfony Forms?
我的應用程序中有一個名為Content
的模型。 它具有三個屬性,這些屬性鏈接到另一個名為ContentBodyType
模型。 這是為了允許內容的三個部分,分別是“橫幅”,“摘要”和“主要”。
我已經使用SymfonyForms為Content
模型創建了一個FormType
。 下面顯示了它是如何構建的:
public function buildForm(FormBuilderInterface $builder, array $options) {
$list = $this -> app['model.collection'] -> getList();
$builder -> add('id', HiddenType::Class)
-> add('displayname', TextType::Class, array("label" => "Friendly Name", "trim" => true))
-> add('description', TextareaType::Class)
-> add('collection', ChoiceType::Class, array(
'choices_as_values' => true,
"choices" => $list,
"label" => "Collection"
))
-> add('publish', ChoiceType::Class, array(
"choices" => array('Yes' => true, 'No' => false),
"choices_as_values" => true,
"expanded" => true,
"multiple" => false,
))
-> add('homepage', ChoiceType::Class, array(
"choices" => array('Yes' => true, 'No' => false),
"choices_as_values" => true,
"expanded" => true,
"multiple" => false,
))
-> add('startdate', DateTimeType::Class, array("widget" => "single_text", "format" => "dd-MM-yyyy"))
-> add('expirydate', DateTimeType::Class, array("widget" => "single_text", "format" => "dd-MM-yyyy"))
-> add('type', ChoiceType::Class, array(
"choices_as_values" => true,
"choices" => array(
'News' => "news",
'Page' => "page",
'Pinned' => "pinned",
'Blog' => "blog",
'Slider' => "slider"
),
"label" => "Type"
))
-> add('main', new \Turtle\Form\Type\ContentBodyType())
-> add('banner', new \Turtle\Form\Type\ContentBodyType())
-> add('summary', new \Turtle\Form\Type\ContentBodyType())
-> add('save', SubmitType::Class)
-> add('cancel', ButtonType::Class);
// only add the name field if this is a new template
if (!$this -> content || null == $this -> content -> getId()) {
$builder -> add('name', TextType::Class, array('label' => 'Name'));
}
}
當應用程序加載表單時,它將在正確的字段中顯示Content
模型中的數據,但是不會顯示嵌套表單中的數據。
現在,我知道這是因為對於“橫幅”,“摘要”和“主”中的每一個,我都在創建ContentBodyType
表單對象的新實例。 但是,我無法解決如何將對象傳遞給此對象,從而不必創建它。
該表單是使用以下代碼創建的:
public function edit(Request $request, $id) {
// Get the entity from the datastore for the specified id
if ($id == "new") {
$item = new \Turtle\Model\Entity\Content();
} else {
$item = $this -> app['model.content'] -> getById($id) -> first();
}
if (!$item) {
return "notthere";
}
// Build the form
$type = new \Turtle\Form\Type\ContentType($this -> app, $item);
$form = $this -> app['form.factory']
-> createBuilder(
$type,
$item,
array(
'action' => $this -> app['url_generator'] -> generate(
'content_update',
array(
'id' => $item -> getId()
)
)
)
) -> getForm();
// process the form to see if it has been submitted or not
$form -> handleRequest($request);
snip ....
我確信這是一個簡單的修復程序,但我只是看不到它是什么。
謝謝,羅素
我已經解決了。 我已經忘記了,當我創建ContentBody
的實例時,我讓構造函數接受了參數,其中之一就是從數據存儲區加載的項目。
class ContentType extends AbstractType {
private $app;
private $item;
public function __construct($app, $item) {
$this -> app = $app;
$this -> item = $item;
}
snip....
因此,由於我將該項目作為類的屬性,因此可以將其傳遞給buildForm
函數中的ContentBodyType
。 因此,現在可以創建表單的三個部分:
-> add('main', new \Turtle\Form\Type\ContentBodyType($this -> item -> getBanner()))
-> add('banner', new \Turtle\Form\Type\ContentBodyType($this -> item -> getSummary()))
-> add('summary', new \Turtle\Form\Type\ContentBodyType($this -> item -> getMain()))
我不確定這是否是正確的方法,因為我確定在SymfonyForms
代碼中的某個時刻模型會傳遞給FormType,但這解決了我遇到的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.