-->

Default Symfony User Entity with Form Class

2019-09-02 18:07发布

问题:

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

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

Now i wish to turn that html intoa Form class wich 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 now 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 bea login form, not crete 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 dont want to use FOSUserBundle yet, i wish to learn the default Smyfony 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 gota a new prblem:

If i try to submit my fomr al i get back is Bad credentials creditals.

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

回答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.