【发布时间】: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