我使用Guzzle從我的Symfony 2應用程序中的API獲取一些數據。這些數據正確映射到我的模型中。該模型是具有一些屬性的Contact
對象。其中一個屬性是ContactMethod
數組這樣Symfony2自定義對象數組到表單域
/**
* @SerializedName("methods")
* @Type("array<My\Bundle\Model\ContactMethod>")
*/
private $methods;
的ContactMethod
對象做只有兩個屬性:type
和value
class ContactMethod {
/**
* @SerializedName("type")
* @Type("string")
*/
private $type;
/**
* @SerializedName("value")
* @Type("string")
*/
private $value;
... setters and getters ...
我已經創建了其buildForm
功能ContactType
以以表格形式顯示Contact
的全部屬性。但是當涉及到顯示methods
屬性時,我希望TextInput
適用於ContactMethod
中的每一個,並且輸入應該將type
作爲標籤,將value
作爲值。
class ContactType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options = array())
{
$builder
->add(...all the other simple fields...)
->add(...what shall I add here ?)
我還需要這以相反的方式工作:當所有的文本輸入填充並提交,字段必須被一起打包成ContactMethod
一個數組,所以我可以重新序列化和數據發送回API。
我試圖玩弄自定義表單類型和數據轉換器,但我找不到解決方案。