跳到主内容

自定义注册表单

本指南介绍如何使用 Signup items 表构建应用的注册表单。


学习目标​

  • 添加、删除和排序注册表单的字段。
  • 将字段设为必填,并使用正则表达式进行验证。
  • 通过规则更改字段的行为。
  • 在用户登录后询问缺失的字段。

前提条件​

  • 一个已开启 Enable signup 的应用

关于注册项​

Signup items 中的每一行对应注册页面的一个字段或元素。通过 Casdoor SDK 创建的应用会获得默认项:ID、Username、Display name、Password、Confirm password、Email、Phone 和 Agreement。

Signup items table of an application

列​

列值描述
名称—注册项的名称。
可见True / FalseShow or hide on the sign-up page. /api/signup ignores the values of hidden items. See Hidden items.
必填True / False该字段是否为必填。
提示True / FalseWhether to ask for the field after sign-in if the account has no value for it. See Ask for missing fields after sign-in.
正则表达式—用于客户端验证的可选正则表达式。
标签—对于以文本开头的项目,对该字段使用 HTML;否则替换项目标签。
自定义 CSS—此注册项的CSS。
规则规则项目自定义此项目的规则(请参阅下表)。
操作—向上移动、向下移动或删除。

配置表单​

  1. 在 Casdoor 管理控制台中,打开应用的编辑页面。
  2. 在 Signup items 中,添加、删除和排序各项,并为每一项设置 Visible 和 Required。
  3. 为支持规则的项设置规则。参见 Item rules。
  4. 保存应用。

例如,要显示一个无需验证的电子邮件字段,请添加 Email 项并将其规则设置为 No verification。

Signup items configured with an email field without verification

注册页面随后如下所示:

Sign-up page with an email field

项目规则​

项目规则描述
IDRandom / Incremental用户ID生成:随机或递增。
显示名称无 / 真实姓名 / 名,姓如何显示显示名称;名、姓分别显示名和姓。
电子邮件正常 / 无需验证正常= 需要邮箱验证;无需验证= 跳过验证。
PhoneNormal / No verificationNormal = require phone verification; No verification = skip verification.
协议None (shown as Only signup) / Signin / Signin (Default True)The checkbox for the terms of use always starts checked, whatever the rule. If the item is required and the user unchecks it, submitting the form or clicking a provider button shows "Please accept the agreement!". To show the terms of use on the sign-in page, use the Agreement sign-in item.
语言None / Label在注册页上添加语言选择器。 None = 单独显示选择器;Label = 带文字标签显示。

让用户选择标签​

Tag 项添加了一个列表,用户在注册时从中选择自己的标签。它默认隐藏。开启 Visible 以显示它。

  • 选项来自该项目的 Options。如果该项目没有选项,则来自应用的 Tags。
  • 用户选择的标签优先于应用的 Default tag,后者仅在用户未选择任何标签时生效。

使用正则表达式验证输入​

  1. 在该项目的行中,将 Regex 设置为一个模式,例如 ^[a-zA-Z0-9_]+$ 用于仅包含字母、数字和下划线的用户名。
  2. 保存应用。

当输入不匹配时,注册页面会在提交前显示错误。此验证适用于 Username、Display name、First name、Last name、Affiliation 和自定义字段。

隐藏的项目​

在验证注册请求之前,POST /api/signup 会清除 Signup items 中缺失或 Visible 关闭的所有字段。调用 API 的客户端无法设置应用注册页面未显示的字段。

ItemFields that Casdoor clears when the item is hidden
Usernameusername
Display namename, firstName, lastName
First namefirstName
Last namelastName
Passwordpassword
Emailemail, emailCode
Phonephone, countryCode, phoneCode
Email or Phone, Phone or Emailemail, emailCode, phone, countryCode, phoneCode
Country/Regionregion
ID cardidCard
Affiliationaffiliation
Biobio
Tagtag
Educationeducation
Gendergender
Languageslanguage

由多个项目拥有的字段(如 firstName),只要其中任何一个项目可见,就会保留。

登录后询问缺失字段​

启用 Prompted 后,Casdoor 会向用户询问其账户中缺失的字段,例如那些通过不提供电话号码的提供商注册的用户。

  1. 在 Display name、Email、Phone 或 Country/Region 的行中,开启 Visible 和 Prompted。
  2. 保存应用。

对于 Affiliation,当 Visible 关闭时,Prompted 开关仍然出现,且只要 Prompted 开启,提示页面就会询问归属机构。在 Providers 表中,提供商的 Prompted 列以相同的方式要求用户关联该提供商。

当用户通过 OAuth 2.0 授权码流程登录到应用时(包括通过第三方提供商登录),Casdoor 会在重定向到客户端之前检查账户。如果某个被提示的字段为空,用户将首先转到提示页面 /prompt/<application>。如果所有字段都有值,用户将直接跳转到重定向 URL。

在提示页面上:

  • 新增或更改的电子邮件地址或电话号码需要验证码。Casdoor 仅在验证代码通过后才保存它。电话号码使用选定的国家/地区代码,默认为组织的第一个国家/地区代码。
  • 在提示的所有字段都有值且每个更改的电子邮件地址或电话号码都有代码之前,Submit and complete 按钮保持禁用状态。

使用电子邮件地址作为用户名​

如果组织启用了 Use Email as username 并且隐藏了 Username 项,则用户的电子邮件地址将成为用户名。参见 Organizations。

另请参阅​