2015-09-04 69 views
1

我剛剛開始使用Meteor和autoform。我創建了quickform調用的Schema。問題是我無法弄清楚如何驗證在沒有數組組包裝的特定數組索引上工作。我可以驗證它,如果我在下面使用這種類型的模式,但它然後它需要一個對象,我正在尋找一個字符串。如果將類型更改爲字符串,則驗證完全不顯示。任何幫助是極大的讚賞。電子郵件中的流星Collection2驗證顯示爲對象但不是字符串

schema.js

Schema.NewUser = new SimpleSchema({ 
    "profile.organization" : { 
     type: String, 
     regEx: /^[a-z0-9A-z .]{3,30}$/, 
     optional: true, 
     label: "Company" 
    }, 
    emails: { 
     type: Object, 
     label: "Email", 
    }, 
    "emails.$":{ 
    type: Object, 
    }, 
    "emails.$.address": { 
    type: String, 
    label: "Email", 
    regEx: SimpleSchema.RegEx.Email, 
    }, 
    parent: { 
    type: String, 
    optional: true, 
    }, 
    roles: { 
    type: Array, 
    optional: true 
    }, 
    'roles.$': { 
     type: String, 
     allowedValues: [ 
     'owner', 
     'admin' 
     ], 
     optional: true, 
     label: "Choose a number", 
     autoform: { 
     options: [ 
      { 
       label: "owner", 
       value: "owner" 
      }, 
      { 
       label: "admin", 
       value: "admin" 
      } 
     ] 
     } 
    } 
}); 

HTML

{{> quickForm collection="Meteor.users" id="insertUserForm" type="method" meteormethod="insertUser" schema="Schema.NewUser" fields="profile.organization, emails.0.address, roles.0" }} 
+0

您期待哪個字段的驗證發生? –

+0

我想要email.0.address進行驗證。我想顯示它作爲一個單獨的領域沒有框和加減號。如果我顯示電子郵件驗證工作,但是如果我執行email.0.address,它只會顯示在控制檯中。謝謝您的幫助 –

回答

1

我結束了由quickField切換與afFieldInput到自動窗體搞清楚了這一點。這可以選擇檢查字段是否與afFieldIsInvalid一致。我用它來檢查父郵件字段進行驗證,但仍然使用索引特定電子郵件作爲我的輸入。

它結束了看起來像這樣

{{#autoForm collection="Meteor.users" id="insertUserForm" type="method" meteormethod="insertUser" schema="Schema.NewUser" }} 
{{> afQuickField name='profile.organization'}} 

<div class="form-group{{#if afFieldIsInvalid name='emails'}} has-error{{/if}}"> 
    <label>Email</label> 
    {{> afFieldInput name='emails.0.address'}} 

    {{#if afFieldIsInvalid name='emails'}} 
    <span class="help-block">{{afFieldMessage name='emails'}}</span> 
    {{/if}} 
</div> 

<label>Roles</label> 
{{> afFieldInput name="roles.0" options=roleOptions}} 

<button type="submit" class="btn btn-primary">Insert</button> 
{{/autoForm}} 

在我的模式我做了小改動電子郵件。電子郵件類型現在是[對象]。

emails: { 
     type: [Object], 
     label: "Email", 
     optional: false 
    }, 
    "emails.$.address": { 
     type: String, 
     regEx: SimpleSchema.RegEx.Email, 
     label: 'Email' 
    }