# 字段唯一性验证

**URL:** <https://meta.discourse.org/t/field-uniqueness-validation/183622>\
**Category:** Support\
**Created:** [2021年三月18日 07:33 UTC](https://meta.discourse.org/t/field-uniqueness-validation/183622 "2021-03-18T07:33:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![PBS](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pbs/32/209265_2.png) [@PBS](https://meta.discourse.org/u/PBS)\
**Post date:** [2021年三月18日 07:33 UTC](https://meta.discourse.org/t/field-uniqueness-validation/183622/1 "2021-03-18T07:33:07Z")

</div>

大家好，

我已在个人资料中添加了一个自定义字段，并将其设为必填项。因此，用户必须在注册时填写该字段。我需要像处理电子邮件字段那样，在数据库中检查该字段是否唯一，并显示类似“该主要邮箱已被占用”的提示。

有哪些可行的实现方式？目前我不打算使用 SSO。

谢谢，  
Prashanth

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2021年三月18日 10:25 UTC](https://meta.discourse.org/t/field-uniqueness-validation/183622/2 "2021-03-18T10:25:56Z")

</div>

我无法想象一个不需要插件的解决方案。不过，有了插件，这并不难。

---

<div class="post-metadata">

**Author:** ![PBS](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pbs/32/209265_2.png) [@PBS](https://meta.discourse.org/u/PBS)\
**Post date:** [2021年三月18日 11:17 UTC](https://meta.discourse.org/t/field-uniqueness-validation/183622/3 "2021-03-18T11:17:30Z")

</div>

感谢 @pfaffman。看来是时候开始探索编写自定义插件了。这需要大量的 Ruby 知识吗？我熟悉 Java/J2EE/.NET，前端技术栈也略知一二。

谢谢，  
Prashanth

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2021年三月18日 16:13 UTC](https://meta.discourse.org/t/field-uniqueness-validation/183622/4 "2021-03-18T16:13:08Z")

</div>

> [@PBS](#):
>
> 需要很多 Ruby 知识吗？

你大概能理解 Ruby 的大部分用法，尽管它有一些独特的简写方式。真正需要稍加学习的是 Rails，不过你应该能找到一些示例，即使没有深入掌握相关概念也能跟着操作。

虽然我不能打包票，但我会留意是否有能帮上忙的资源。
