Symfony 2.7:访问未在表单对象中呈现 属性
Symfony 2.7: Access not rendered property within the form-object
我需要访问表单对象中的 属性。问题是,我想访问的 属性 没有在表单中呈现,也没有在 contractType class 中声明。
class ContractType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rcode1', new TextType(), array('label' => 'rcode 1'))
->add('rcode2', new TextType(), array('label' => 'rcode 2'));
}
...
}
转储表单对象:
array:6 [▼
"contract" => Contract {#2003 ▼
- id: null
- actionCode: "104"
- productCode: "20106"
- created: null
- updated: null
- resumeId: null
- rcode1: null
- rcode2: null
- downloadId: null
- businessContractDetails: BusinessContractDetails {#1999 ▶}
- privateContractDetails: null
- company: Company {#2000 ▶}
- persons: ArrayCollection {#1998 ▶}
}
"businessContractDetails" => BusinessContractDetails {#1999 ▶}
"company" => Company {#2000 ▶}
"contactPerson" => ContactPerson {#1987 ▶}
"landlord" => Landlord {#1993 ▶}
"businessRealEstate" => BusinessRealEstate {#1994 ▶}
]
合同实体呈现的属性是rcode1和rcode2。但我需要访问 downloadID。
我试过了
$form->get('contract')->get('downloadId')->getData();
并收到以下错误消息:子 "downloadId" 不存在。
有什么建议吗?提前致谢!
尝试
$form->get('contract')->getData()->getDownloadId();
请注意,我假设您的 Contract
class
中有一个 getter getDownloadId
您的控制器不应通过表单访问实体的值。它会起作用,但这是一种不好的做法,例如,如果表单类型 contract
的名称发生变化。
您可以依赖于您创建表单的实体,在 handleRequest
该实体被表单中的数据填充后。所以:
# Controller POST action
$entity = new Contract(); // Or retrieve it from database: $em->find(Contract, $id);
$form = $this->createForm(FormType::class, $entity)->handleRequest($request);
echo $entity->getDownloadId();
我需要访问表单对象中的 属性。问题是,我想访问的 属性 没有在表单中呈现,也没有在 contractType class 中声明。
class ContractType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rcode1', new TextType(), array('label' => 'rcode 1'))
->add('rcode2', new TextType(), array('label' => 'rcode 2'));
}
...
}
转储表单对象:
array:6 [▼
"contract" => Contract {#2003 ▼
- id: null
- actionCode: "104"
- productCode: "20106"
- created: null
- updated: null
- resumeId: null
- rcode1: null
- rcode2: null
- downloadId: null
- businessContractDetails: BusinessContractDetails {#1999 ▶}
- privateContractDetails: null
- company: Company {#2000 ▶}
- persons: ArrayCollection {#1998 ▶}
}
"businessContractDetails" => BusinessContractDetails {#1999 ▶}
"company" => Company {#2000 ▶}
"contactPerson" => ContactPerson {#1987 ▶}
"landlord" => Landlord {#1993 ▶}
"businessRealEstate" => BusinessRealEstate {#1994 ▶}
]
合同实体呈现的属性是rcode1和rcode2。但我需要访问 downloadID。
我试过了
$form->get('contract')->get('downloadId')->getData();
并收到以下错误消息:子 "downloadId" 不存在。
有什么建议吗?提前致谢!
尝试
$form->get('contract')->getData()->getDownloadId();
请注意,我假设您的 Contract
class
getDownloadId
您的控制器不应通过表单访问实体的值。它会起作用,但这是一种不好的做法,例如,如果表单类型 contract
的名称发生变化。
您可以依赖于您创建表单的实体,在 handleRequest
该实体被表单中的数据填充后。所以:
# Controller POST action
$entity = new Contract(); // Or retrieve it from database: $em->find(Contract, $id);
$form = $this->createForm(FormType::class, $entity)->handleRequest($request);
echo $entity->getDownloadId();