Reputation: 413
I'm learning Symfony2 and I'm blocked at a point.
I did a form and I can get the values but I don't know how to get a field of an object
Code for creating the form :
$form = $this->createFormBuilder()
->add('type', 'choice', array('choices' => array('o' => 'Invoice','v' => 'Reconciliation')))
->add('clients','entity',array(
'class' => 'PVRecsBundle:Client',
'query_builder' => function(EntityRepository $er){
return $er->createQueryBuilder('c')
->orderBy('c.legalcompanyname','ASC');
},
'property' => 'legalCompanyName',
'expanded' => false,
'multiple' => false))
->add('dates','date',array('widget' => 'choice', 'input' => 'timestamp'))
->add('save', 'submit')
->getForm();
$form->handleRequest($request);
And the code for getting the datas :
$request = $this->get('request');
if ($request->getMethod() == 'POST')
{
$data = $form->get('clients')->getData();
dump($data);
}
I get the right datas but I get all the fields of my client.
the dump of my var :
Client {#654 ▼
-id: 11334
-name: 101579
-identifiercdr: 101579
-vatnumber: ""
-einnumber: ""
....
}
but now how do I get the field identifiercdr per example
The error:
Catchable Fatal Error: Object of class PV\RecsBundle\Entity\Client could not be converted to string
I think it should be something like :
$data = $form->get('clients')['identifiercdr']->getData();
Upvotes: 2
Views: 73
Reputation: 11351
Try:
$client = $form->get('clients')->getData();
$identifiercdr = $client->getIdentifiercdr();
(I am assuming that your Client
entity has a getter for the identifiercdr
field. If it does not, add it)
Upvotes: 1