【问题标题】:Meteor.js Validation w/ MesosphereMesosphere 的 Meteor.js 验证
【发布时间】:2014-01-03 14:24:17
【问题描述】:

我正在尝试在我的流星应用程序中实施 Mesosphere 以进行验证,但 Mesosphere 似乎没有接受我列出的一些本机验证。

我只尝试了一次验证电子邮件格式和所需长度。例如:

Mesosphere({
    name: 'signupForm',
    method: 'signupUser',
    fields: {
        email: {
            required: true,
            format: 'email',
            rules: {
                exactLength: 4
            },
            message: 'Wrong length'
        }
    },
    onFailure: function (errors) {
        messages = [];

        messages = _.map(errors, function (val, err) {
            console.log(val.message);
        });
    },
    onSuccess: function (data) {
        alert("Totally worked!")
    }
});

“onFailure”(和 onSuccess)回调似乎有效,因为它在我提交表单时正在记录某些内容。这让我相信我也在表单提交事件中正确设置了它。如果我理解正确,您可以将表单对象传递给 Mesosphere 以创建验证对象。例如:

var validationObject = Mesosphere.signupForm.validate(accountData);

提交后,它会将 必填字段 记录为错误,这很奇怪,因为我确实在该字段中输入了一些内容。它没有提到不正确的长度或格式。它会跳过“错误长度”消息,我无法在对象中的任何地方找到该消息。

所以我的问题是我做错了什么,没有为该表单字段的错误输入获得正确的消息?谢谢:)

此外,愿意就其他验证包提出建议。 Mesosphere 利用 Meteor 的服务器/客户端功能进行验证,因此它似乎是一个不错的起点。

模板:

<template name="signup">
      <form name="signupForm" id="signup-form" class="panel" action="#">
        <div class="form-group">
          <label for="email">Email</label>
          <input type="text" name="email" id="email" class="form-control" />
        </div>

        <div class="form-group">
          <label for="password">Password</label>
          <input type="password" name="password" id="password" class="form-control" />
        </div>

        <div class="form-group">
          <input type="submit" value="Sign Up" id="create-account" class="btn btn-success pull-right">
        </div>
      </form> </template>

在对应文件中调用该方法:

  signupUser: function(accountData) {
    var uid = Accounts.createUser(accountData);
  }

【问题讨论】:

  • 您可以将模板 html 添加到您的问题中吗?
  • 添加了模板和对应的方法。谢谢大卫。
  • 我花了大约 20 分钟从包中的示例向后工作,但我没有弄明白。它在 5 个月内没有更新,所以这是一个危险信号。你可能想看看autoform。我没有尝试过,因为我们编写了自己的验证库(尚未公开)。
  • Autoform 很有意义。感谢 David 的推荐,并感谢您尝试使用 Mesosphere。很高兴我不是唯一一个在这方面有问题的人。

标签: javascript validation meteor mesosphere


【解决方案1】:

所以基本上我在这里看到的是您的规则没有反映表单的验证方式。您有一封必须与电子邮件格式匹配的电子邮件,但您有一条规则说它必须恰好是 4 个字符长.. 更好的字段定义如下所示:

fields: {
    email: {
        required: true,
        format: 'email',
        message: 'Please enter a valid email address'
    },
    password: {
        required: true,
        rules: {
            minLength: 6,
            maxLength: 30,
        },
        message: "Password must be between 6 and 30 characters"
    }
}

我创建了一个 github 存储库,如果你愿意,你可以克隆并运行它来测试它。

https://github.com/copleykj/meso-test

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多