[英]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"
該messages
和en
哈希值是必要的它是正確的結構。
要點 : custom
的返回值不是在客戶端上顯示的消息。 它是一個密鑰 ,指向默認消息對象中的條目。
例如:
custom: ->
if @obj.content.includes("a")
true
else
"notA"
這將最終顯示消息"doesnt contain a"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.