3

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')

    ;
}
1
  • how do you create the form? Commented May 30, 2015 at 12:08

3 Answers 3

6

Replace

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

with

$username = $form["user"]["username"]->getData();
Sign up to request clarification or add additional context in comments.

2 Comments

["user"] before ["username"] thank you ;) $username = $form["user"]["username"]->getData();
Indeed, that was a mistake :) Fixed it
1

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.

Comments

1

If that:

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

doesn't work, you can use instead:

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

(tested in SF 4-5)

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.