hous
hous

Reputation: 2679

Get form values in Symfony2 controller , Child does not exist

This is how the source code of input username is :

<input type="text" id="user_username" name="user[username]" >

When I try to get it in the controller I get this error :

Child "username" does not exist

Controllor :

//.......
if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);
        $i = 0;
        $username = $form["username"]->getData();
        $user= $em->getRepository('UsersBundle:User')->findOneByUsername($username);

//.......
}

This is th formType

class EleveType extends AbstractType
{
 public function buildForm(FormBuilderInterface $builder, array $options)
   {
    $builder
        ->add('user', new UserType())
        ->add('ecole')
        ->add('niveauscolaire')

    ;
}

Upvotes: 3

Views: 7786

Answers (3)

Hu̅g&#248;
Hu̅g&#248;

Reputation: 9

If that:

$username = $form["user"]["username"]->getData();

doesn't work, you can use instead:

$username = $form['user']->getData()->getUsername();

(tested in SF 4-5)

Upvotes: 1

Jean-Luc Barat
Jean-Luc Barat

Reputation: 1165

In my case, I need to use:

$username = $form["user"]["username"]->getViewData();

instead of:

$username = $form["user"]["username"]->getData();

Thanks for @Jacob orientation which help me and sorry for my shinny english.

Upvotes: 1

Jacob
Jacob

Reputation: 1793

Replace

$username = $form["username"]->getData();

with

$username = $form["user"]["username"]->getData();

Upvotes: 6

Related Questions