0

I wish to create A Form class for my Login form that uses the Default Symfony User class.

I followed the book here: http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

Now I wish to turn that html into a Form class for which I did:

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->add('_username', 'text');
        $builder->add('_password', 'password');
    }
    
    public function getName()
    {
        return 'login';
    }
    
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
                'data_class' => 'Symfony\Component\Security\Core\User\User',
        ));
    }
}

this should work.

Now I try to call it like this:

$user = new \Symfony\Component\Security\Core\User\User('ana', 'anon');
$form = $this->createForm(new LoginForm(), $user);

And I got 2 problems:

  1. 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.

  2. it throws an error when I try to render:

    Neither property "_username" nor method "get_username()" nor method 
    "is_username()" exists in class "Symfony\Component\Security\Core\User\User"
    500 Internal Server Error - InvalidPropertyException
    

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.

EDIT: 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:

If I try to submit my form all I get back is Bad credentials credentials.

login.html.twig:

<form action="{{ path('main_login_check') }}" method="post">
    {{ form_widget(form) }}

    <button type="submit">login</button>
</form>

this is the Action that calls it:

public function loginAction()
    {
        $request = $this->getRequest();
        $session = $request->getSession();
        $user = new \Symfony\Component\Security\Core\User\User('ana', 'anon');
        $form = $this->createForm(new LoginForm(), $user);
        // get the login error if there is one
        if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR))
        {
            $error = $request->attributes->get(
                    SecurityContext::AUTHENTICATION_ERROR
                    );
        }
        else 
        {
            $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
            $session->remove(SecurityContext::AUTHENTICATION_ERROR);
        }
        
        return $this->render(
                'BattlemamonoMainBundle:Security:login.html.twig',
                array(
                        // last username entered by the user
                        'last_username'   => $session->get(SecurityContext::LAST_USERNAME),
                        'error'           => $error,
                        'form'            => $form->createView(),
                        )
                );
    }
5
  • Did you try just 'username' instead of '_username'`? Commented Feb 8, 2013 at 11:42
  • just now and it worked but raiseda new problem. Commented Feb 8, 2013 at 11:43
  • If you go through all this it should actually work symfony.com/doc/current/book/security.html Commented Feb 8, 2013 at 12:18
  • It did. but it didnt use a Form class. And want to use a Form class. that book doesnt explains how i can turn it intoa form class. if it fid i would have not made this question. Commented Feb 8, 2013 at 12:20
  • You don't need to use a form class, there are just two fields... Commented Feb 8, 2013 at 12:37

1 Answer 1

1

The properties are 'username' and 'password' in the core User class, opposed to _username and _password - they'll need to match or you'll need to specify the property name mapping in the form builder to avoid the exception above.

This advice holds for mapping forms on to entities in general.

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

1 Comment

yeah i just did that but i gota new problem..and the first problem still stands...

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.