title: Default Symfony User Entity with Form Class
body: <p>I wish to create A Form class for my Login form that uses the Default Symfony User class.</p>
  <p>I followed the book here: <a href="http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form" rel="nofollow noreferrer">http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form</a></p>
  <p>Now I wish to turn that html into a Form class for which I did:</p>
  <pre><code>use Symfony\Component\Form\AbstractType;
  use Symfony\Component\Form\FormBuilderInterface;
  use Symfony\Component\OptionsResolver\OptionsResolverInterface;
  
  class LoginForm extends AbstractType
  {
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
          $builder-&gt;add('_username', 'text');
          $builder-&gt;add('_password', 'password');
      }
      
      public function getName()
      {
          return 'login';
      }
      
      public function setDefaultOptions(OptionsResolverInterface $resolver)
      {
          $resolver-&gt;setDefaults(array(
                  'data_class' =&gt; 'Symfony\Component\Security\Core\User\User',
          ));
      }
  }
  </code></pre>
  <p>this should work.</p>
  <p>Now I try to call it like this:</p>
  <pre><code>$user = new \Symfony\Component\Security\Core\User\User('ana', 'anon');
  $form = $this-&gt;createForm(new LoginForm(), $user);
  </code></pre>
  <p>And I got 2 problems:</p>
  <ol>
  <li><p>the user class requires that I specify an username and password which is  not what I want at all. I want this to be a login form, not create a user form.</p>
  </li>
  <li><p>it throws an error when I try to render:</p>
  <pre><code>Neither property &quot;_username&quot; nor method &quot;get_username()&quot; nor method 
  &quot;is_username()&quot; exists in class &quot;Symfony\Component\Security\Core\User\User&quot;
  500 Internal Server Error - InvalidPropertyException
  </code></pre>
  </li>
  </ol>
  <p>What am I doing wrong?
  And I don't want to use FOSUserBundle yet, I wish to learn the default Symfony classes, you can call me an idiot for this.</p>
  <p><strong>EDIT:</strong> I removed the the underscores(_) from the field names and it started to work but the first problem still stands, and now I got a a new problem:</p>
  <p>If I try to submit my form all I get back is  <code>Bad credentials</code> credentials.</p>
  <p>login.html.twig:</p>
  <pre><code>&lt;form action=&quot;{{ path('main_login_check') }}&quot; method=&quot;post&quot;&gt;
      {{ form_widget(form) }}
  
      &lt;button type=&quot;submit&quot;&gt;login&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  <p>this is the Action that calls it:</p>
  <pre><code>public function loginAction()
      {
          $request = $this-&gt;getRequest();
          $session = $request-&gt;getSession();
          $user = new \Symfony\Component\Security\Core\User\User('ana', 'anon');
          $form = $this-&gt;createForm(new LoginForm(), $user);
          // get the login error if there is one
          if ($request-&gt;attributes-&gt;has(SecurityContext::AUTHENTICATION_ERROR))
          {
              $error = $request-&gt;attributes-&gt;get(
                      SecurityContext::AUTHENTICATION_ERROR
                      );
          }
          else 
          {
              $error = $session-&gt;get(SecurityContext::AUTHENTICATION_ERROR);
              $session-&gt;remove(SecurityContext::AUTHENTICATION_ERROR);
          }
          
          return $this-&gt;render(
                  'BattlemamonoMainBundle:Security:login.html.twig',
                  array(
                          // last username entered by the user
                          'last_username'   =&gt; $session-&gt;get(SecurityContext::LAST_USERNAME),
                          'error'           =&gt; $error,
                          'form'            =&gt; $form-&gt;createView(),
                          )
                  );
      }
  </code></pre>