我试图在我的AutoForm表单所在的模板中创建一些帮助程序,以更改表单外部的某些元素以反映错误。我尝试过使用
Template.signUp.helpers
isValid: ->
AutoForm.getValidationContext('signUp').isValid()
但是这只会返回以下错误:
Exception in template helper: TypeError: Cannot read property '_resolvedSchema' of undefined
这是模板:
<template name="signUp">
{{#if isValid}} do something {{/if}}
{{#autoForm id="signUp" schema="Schemas.SignUp" type="normal"}}
<div class="input {{#if afFieldIsInvalid name='firstName'}}error{{/if}}">
<label for="firstName">{{data.firstName}}</label>
{{> afFieldInput name="firstName" type="text"}}
</div>
<div class="input {{#if afFieldIsInvalid name='lastName'}}error{{/if}}">
<label for="lastName">{{data.lastName}}</label>
{{> afFieldInput name="lastName" type="text"}}
</div>
<div class="input {{#if afFieldIsInvalid name='email'}}error{{/if}}">
<label for="email">{{data.email}}</label>
{{> afFieldInput name="email" type="email"}}
</div>
<div class="input">
{{> afFieldInput name="optIn" type="boolean-checkbox"}}
</div>
<button type="submit" class="btn full" data-analytics='{"eventCategory":"Sign Up","eventAction":"Click","eventLabel":"{{data.submit}}"}'><span>{{data.submit}}</span></button>
{{/autoForm}}
</div>
</div>
非常感谢任何帮助,谢谢!
因此,经过大量的游戏,我发现当您尝试在AutoForm上下文之外验证AutoForm时,您应该使用SimpleSchema进行验证。
我在这里定义架构并保存命名上下文(coffeescript)
@Schemas = {}
@Schemas.SignUp = new SimpleSchema
firstName:
type: String
optional: false
lastName:
type: String
optional: false
email:
type: String
regEx: SimpleSchema.RegEx.Email
optional: false
optIn:
type: Boolean
label: Localization.signUp.optIn
optional: true
defaultValue: false
@SignUpForm = Schemas.SignUp.namedContext('signUp')
这就是我使用SimpleSchema验证整个表单的地方。
Template.signUp.helpers
isValid: ->
SignUpForm.isValid()
SDS