Search code examples
meteormeteor-autoformmeteor-collection2

Meteor Collection2 validation on emails displays as object but not as string


I am just starting out with Meteor and autoform. I have created a Schema that the quickform calls. Problem is I can't figure out how to get validation to work on a particular array index without the array group wrapper. I can get validation on it if I use this type of schema below but then it requires an object and I am looking for a String. If I change the type to String then validation does not show up at all. Any help is greatly appreciated.

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" }}

Solution

  • I ended up figuring this out by switching from quickField to autoform with afFieldInput. This has the option of checking if the field is valid with afFieldIsInvalid. I used this to check the parent emails field for validation but still used the index specific email as my input.

    It ended up looking like this

    {{#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}}
    

    In my Schema I made small change to emails. Emails type is now [Object].

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