【问题标题】:SimpleSchema and AutoForm: Render password field as single inputSimpleSchema 和 AutoForm:将密码字段呈现为单个输入
【发布时间】:2017-07-04 15:49:15
【问题描述】:

我正在一个项目中工作,其中“超级管理员”用户可以创建其他用户,设置他们的用户名和密码。我有一个 AutoForm quickForm 根据附加到 Meteor.users 集合的 SimpleSchema 呈现表单(使用 Collection2)。 遵循Collection2 docs 将架构附加到用户集合的建议,我的架构如下所示:

Usuarios.schema = new SimpleSchema({
    ...
    username: {
        type: String,
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
   "services.password": {
      type: String,
      optional: true,
      autoform: {
         type: 'password'
      }
    },
    ...
});

呈现的表单如下所示:

但我希望密码字段呈现为用户名之一(没有服务面板)。 我还没有找到解决方法。我需要在架构中有服务对象类型属性,否则用户插入时的验证失败(使用Accounts.createUser()),因此,面板被渲染(因为属性的对象类型)。

关于如何实现所需的模板渲染有什么想法吗?

【问题讨论】:

    标签: meteor meteor-autoform simple-schema meteor-collection2


    【解决方案1】:

    您的密码是“服务”对象的一部分,这就是为什么在使用 quickForm 或 quickFields 时它会自动呈现在 afObjectField 中的原因:

    afObjectField

    当您将 afQuickField 组件用于作为对象的字段时, 它使用 afObjectField 组件呈现,除非您覆盖 类型或指定选项。当您使用 具有 Object 类型字段的架构的 quickForm。

    afObjectField 组件渲染对象字段的所有子字段 一起作为一组。该组标有该组的名称 对象字段。组的实际视觉表现会有所不同 根据您使用的主题模板。对于“bootstrap3”默认 模板,该组出现在一个带有标题的面板中。

    解决方案 A:手动表单呈现

    要将您的密码呈现为单个字段,您需要手动设置您的 autForm 并使用 afFieldInput 引用这些字段。 mthen 的形式可能如下所示(未经测试,但代码应如下所示):

    {{> autoForm id="login" schema=Usuarios.schema}}
        {{> afFieldInput type="text" name="username"}}
        {{> afFieldInput type="password" name="services.password"}}
    {{/autoForm}}
    

    它的优点是您的表单看起来完全如您所愿,但缺点是您必须手动添加所有额外内容(验证消息和内容)。

    解决方案 B:更改架构

    当您从服务组中提取密码时,您将获得与使用用户名相同的自动渲染效果:单个输入字段。

    Usuarios.schema = new SimpleSchema({
        ...
        username: {
            type: String,
        },
        services: {
            type: Object,
            optional: true,
            blackbox: true
        },
        ...
       password: {
          type: String,
          optional: true,
          autoform: {
             type: 'password'
          }
        },
        ...
    });
    

    【讨论】:

    • 解决方案 B 有效!由于我的项目结构,我无法使用解决方案 A。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-04-13
    • 1970-01-01
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多