在 symfony 表单中,我如何验证查询数据库的字段(即密码)以获得正确的值?
新手问题。
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为查看 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.