2

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();

1 Answer 1

1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.