在 symfony 表单中,我如何验证查询数据库的字段(即密码)以获得正确的值?

发布于 2024-09-09 17:56:53 字数 1273 浏览 1 评论 0原文

新手问题。

我在 symfony 中创建了一个非常简单的 LoginForm 类。它检查用户是否在字段中插入用户名和密码,并且它按预期工作。

// UserLoginForm :: configure()

$this->setWidgets(array(
  'username' => new sfWidgetFormInputText(),
  'password' => new sfWidgetFormInputPassword(),
));
$this->setValidators(array(
  'username' => new sfValidatorString(array('required' => true),
  'password' => new sfValidatorString(array('required' => true)
));


// Action :: executeLogin()

$this->loginForm = new UserLoginForm();

if ($request->isMethod('post')) {
    $params = $request->getParameter('login');
    $this->loginForm->bind($request->getParameter('login'));

    if ($this->loginForm->isValid()) {
        // ... more code                
    }
}

现在我试图检查数据库中用户名字段是否存在,如果不存在,我想使表单无效。密码相同:如果用户名+密码对不存在,但显示不同的消息。

尝试找到我以两种可能的方式搜索的解决方案:

在操作中:

  • 查询数据库,
  • 检查用户名,
  • 使表单用户名字段无效(我希望在生成的 HTML 中显示错误消息),
  • 对于用户名+密码检查

在这种情况下我如何使表格无效? 如何处理模板中的错误消息?

在调用 isValid() 方法的表单类中:

  • 某些内容添加到 setValidator()
  • 或添加一个调用查询模型的私有函数的后验证器

在这种情况下如何我可以在 Form 类中使用该模型吗?

哪种方法是最好/最快的? 其他可能性?

Newbie question.

I've created a very simple LoginForm class in symfony. It checks if the user inserts the username and the password in the field and It work as expected.

// UserLoginForm :: configure()

$this->setWidgets(array(
  'username' => new sfWidgetFormInputText(),
  'password' => new sfWidgetFormInputPassword(),
));
$this->setValidators(array(
  'username' => new sfValidatorString(array('required' => true),
  'password' => new sfValidatorString(array('required' => true)
));


// Action :: executeLogin()

$this->loginForm = new UserLoginForm();

if ($request->isMethod('post')) {
    $params = $request->getParameter('login');
    $this->loginForm->bind($request->getParameter('login'));

    if ($this->loginForm->isValid()) {
        // ... more code                
    }
}

Now I'm trying to check the presence of the username field in the db and if it does not exists, I want to invalidate the form. The same for the password: if the username + password pair doesn't exist, but with a different message.

Trying to find the solution I searched in two possible ways:

In the action :

  • querying the db,
  • check the username,
  • invalidate the form username field (I want the error message in the resulting HTML),
  • same for the username + password check

In this case how I can invalidate the form ?
How to handle the error message in the template ?

In the form class calling the isValid() method :

  • add something to the setValidator()
  • OR add a post validator that call a private function which query the model

In this case how can I use the model in the Form class ?

Which is the best/fast approach ?
Other possibilities ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦情居士 2024-09-16 17:56:53

我认为查看 sfDoctrineGuardPlugin。他们创建该帖子验证器,并 在表单中设置。即使您不想使用 sfDoctrineGuardPlugin(如果您使用 Propel,则使用 sfGuardPlugin),它的代码也可以成为宝贵的灵感来源。

I think it will be very useful to you to look at the code of the validator of sfDoctrineGuardPlugin. They create that post validator, and set it in the form. Even if you don't want to use sfDoctrineGuardPlugin (or sfGuardPlugin if you use Propel) its code can be a valuable source of inspiration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文