繁体   English   中英

SimpleSchema自定义验证消息未显示

[英]SimpleSchema custom validation message not showing

我正在尝试实现一个自定义验证功能,该功能可以返回true (如果字段有效)或某些自定义错误消息。 这是我目前的尝试:

global.Messages = Models.Messages = new Mongo.Collection 'messages'

MessagesSchema = new SimpleSchema({
  content: {
    type: String,
    label: "Message",
    max: 200,
    custom: ->
      if @obj.content.includes("a")
        true
      else
        "not contain a"
}, {tracker: Tracker})

Messages.attachSchema MessagesSchema

这是一个人为的示例,但是仍然无法正常工作。 运行custom函数中的条件,并在返回true时保存记录。 但是,如果返回"not contain a" ,则它不会成为客户端上显示的验证消息。 它只是说content is invalid ,而且我不确定如何自定义此消息。 这是模板代码:

  {{#autoForm collection="Messages" id="insertMessageForm" type="insert"}}
    <fieldset>
      <legend>Add message</legend>
      {{> afFieldInput type='text' name='content'}}
      {{#if afFieldIsInvalid name='content'}}
      <span class="help-block">{{afFieldMessage name='content'}}</span>
      {{/if}}
    </fieldset>
    <button type='submit' class='btn btn-primary'>Insert</button>
  {{/autoForm}}

我的原始代码存在一些问题。

首先,我没有指定我需要SimpleSchema但是应该通过这种方式完成; 它使用了新的node-simpl-schema包,这是meteor-simple-schema迁移到的包:

SimpleSchema = require('simpl-schema').default
SimpleSchema.extendOptions(['autoform']);

验证消息映射到密钥:

SimpleSchema.setDefaultMessages
  messages:
    en: 
      "notA": "doesnt contain a"

messagesen哈希值是必要的它是正确的结构。

要点custom的返回值不是在客户端上显示的消息。 它是一个密钥 ,指向默认消息对象中的条目。

例如:

custom: ->
  if @obj.content.includes("a")
    true
  else
    "notA"

这将最终显示消息"doesnt contain a"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM