自定义用户字段 - 字段验证

Unsure if I’ve missed it, but the custom User Fields, you can set a field type, bu is their anyway to verify on these fields, such as for a mobile number or via regex so we can verify against any data?

If not currently, could this be added?

2 个赞

There isn’t much point in adding phone number verification. It’s easy for a bad actor to generate a bogus phone number with the right number of digits and highly annoying when a legitimate user has to guess what phone number format someone thinks is the right one.

1 个赞

That was just an example of one field, we have multiple fields to store such as memberships. And it we need some sort of verification that the information is even remotely correct. Currently for a phone number you can enter text.

We should be able to use regex to verify these fields or they are pretty useless.

2 个赞

Also, on the point of mobile numbers, yes they can be fake, but for our use case of the forum, its for their benefit that is correct. And being able to perform some sort of verification means that our scripts that would use them wouldn’t be affected by entering letters instead of numbers for example.

One the second point of different formats, that easy enough where you give the field a description field so you can let them know that. Verification of custom fields is a very basic requirement.

2 个赞

If you want that kind of detail consider an external membership platform and IdM.

This isn’t the kind of thing that discussion software should be doing.

1 个赞

Again, the point isn’t what I want to do with it, but more the fact that custom fields should be able to be verified.

There is no point in them if you can’t somehow verify the data going into them.

How much do you verify the name and identity of your users?

A lot, we’re a closed community. We are only able to verify on email at the moment compared to our current system, but as this can change on both sides, we want them to be able to enter a membership number and mobile and verify on both of those as well. At least if the verification of the field allows us to ensure the info entered is of the right type and length, our scripts using the API can handle the rest.

So you’re not talking about verifying anything, just validating that the data meets an expected format.

Discourse isn’t going to do this for you, you might be able to achieve validation with a plugin, but even then there’s nothing to prevent someone entering bad data in an expected format just to bypass said validation.

Your only real option is to look at something off-site or create a genuine incentive for users to provide real data. Right now you’re fixating on a symptom (not getting the data you want), not the root of the problem (giving users a real and compelling reason to provide it).

添加验证功能会非常有帮助——尤其是针对电话号码,人们似乎总是搞不定前面的加号。是的,我们可以直接整理数据库,但这实在太麻烦了。

2 个赞

完全同意。只要能根据格式、长度和字符验证字段,甚至允许字段匹配特定的正则表达式,就意味着你确保获取了正确类型的数据,这样 API 脚本就能无误地处理。

很抱歉这么晚才发布这条内容——不过,对于那些正在 Meta 上搜索如何验证可能已添加的自定义字段的人来说,我想提一下如何通过插件来实现这一点。

在你的 plugin.rb 文件中,对 User 使用 validate 方法。然后你可以检查自定义字段,并在任何字段无效时设置错误:

require_dependency 'user'

validate User, :my_cool_validation_method do
  field = self.custom_fields['cool_tapes']
  if ! field.blank? && field.include?("bag of four grapes")
    self.errors[:cool_tapes] << "No rhymes allowed."
  end
end

注意::my_cool_validation_method 符号可以是任意名称,但在 User 模型的上下文中必须唯一。

10 个赞

这可以用于在注册时以及在用户个人资料中编辑时验证用户自定义字段吗?

另外,这可以以某种方式放入主题组件中吗?

1 个赞