Zend 表单验证器回调:如何排除用户名? [英] Zend Form Validator Callback: How to exclude a username?

查看:31
本文介绍了Zend 表单验证器回调:如何排除用户名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 User Mapper 类 userExist($username) 中编写了一个函数,如果我的数据库表中不存在相同的用户名,它 return true .现在我想在扩展 Zend_Form 的 Add_User_Form 类中使用这个函数.

I have written one function in User Mapper class userExist($username) which return true if same username does not exist in my database table. Now I want to use this function in the Add_User_Form class which extends Zend_Form.

我想使用:

$username = $this->createElement('text','username');
$username->setLabel('Username:')
         ->addValidator('Callback', true, array('callback'=>userExist???));

我可以使用映射器类 userExist($username) 函数作为回调函数吗?如果我的实现有问题,请告诉我.如果用户已经注册并阻止表单提交,我只想显示错误消息.

Can I use mapper class userExist($username) function as callback function? Please let me know if I am doing something wrong with my implementation. I just want to show error messages if a user is already registered and prevent form submission.

谢谢.

推荐答案

您可以使用标准验证器,而不是使用回调验证器:

Instead of using a callback validator, you can use a standard validator:

$element->addValidator('Db_NoRecordExists', true, array('users', 'username'))

Db_NoRecordExists 允许您检查记录是否存在于数据库的特定表.

Db_NoRecordExists allows you to check whether a record exists in a specific table of your database.

如果用户想要编辑他的个人资料,您可能需要检查记录是否存在除了自身.那么你应该使用相同的验证器,如下所示:

If a user want to edit his profile, you might want to check if a record exists except itself. Then you should use the same validator as follow:

$where = array('users', 'username', array('field' => 'username', 'value' => $username));
$element->addValidator('Db_NoRecordExists', true, $where)

在这些示例中,users 是您的数据库表的名称.username 是您的列名,$username 是您要排除的用户名.

In these example, users is the name of your database table. username is your column name and $username the username you want to exclude.

这篇关于Zend 表单验证器回调:如何排除用户名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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