带有表单类的默认 Symfony 用户实体 [英] Default Symfony User Entity with Form Class

查看:17
本文介绍了带有表单类的默认 Symfony 用户实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望为我的登录表单创建一个使用默认 Symfony 用户类的表单类.

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

我在这里关注了这本书:http://symfony.com/doc/current/book/security.html#using-a-traditional-login-form

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

现在我希望将该 html 转换为我所做的 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',
        ));
    }
}

这应该有效.

现在我试着这样称呼它:

Now I try to call it like this:

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

我遇到了两个问题:

  1. 用户类要求我指定一个用户名和密码,这根本不是我想要的.我希望这是一个登录表单,而不是创建一个用户表单.

  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.

当我尝试渲染时它会抛出一个错误:

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

我做错了什么?而且我还不想使用 FOSUserBundle,我想学习默认的 Symfony 类,你可以为此骂我白痴.

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.

我从字段名称中删除了下划线(_),它开始工作,但第一个问题仍然存在,现在我遇到了一个新问题:

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:

login.html.twig:

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

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

这是调用它的操作:

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

推荐答案

核心 User 类中的属性是 'username' 和 'password',而不是 _username 和 _password - 它们需要匹配,否则你需要在表单生成器中指定属性名称映射以避免上述异常.

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.

这篇关于带有表单类的默认 Symfony 用户实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆