WalterB
WalterB

Reputation: 110

Subschema as array item with AutoForm, SimpleSchema

I am trying to get subschemas to work as an array, which I assume is the correct way to handle my problem (but please correct me if I am wrong!). I provide a simplified working example to show my problem, based on the BooksSchema example provided by the AutoForm package. In my example, I have a collection of Libraries, and one of the fields in the 'Libraries' object is supposed to be the library's collection of books. Rendering the AutoForm does not give me any input labels as defined in my Book collection, but instead just shows one (1) empty text input field.

Schemas:

import SimpleSchema from 'simpl-schema';
SimpleSchema.extendOptions(['autoform']);

BooksSchema = new SimpleSchema({
  title: {
    type: String,
    label: "Title",
    max: 200
  },
  author: {
    type: String,
    label: "Author"
  },
  copies: {
    type: Number,
    label: "Number of copies",
    min: 0
  },
  lastCheckedOut: {
    type: Date,
    label: "Last date this book was checked out",
    optional: true
  },
  summary: {
    type: String,
    label: "Brief summary",
    optional: true,
    max: 1000
  }
}, { tracker: Tracker });

LibrariesSchema = new SimpleSchema({
  collection: {
    type: Array
  },
  'collection.$': {
      type: BooksSchema,
      minCount: 1
  }
});

LibrariesSchema.extend(BooksSchema);


Libraries = new Mongo.Collection("libraries");
Libraries.attachSchema(LibrariesSchema);

AutoForm:

  {{> quickForm collection="Libraries" id="insertBookForm" type="insert"}}

Thank you so much in advance for your time, really been struggling with this for a long time now!

Upvotes: 1

Views: 832

Answers (2)

adamtheray
adamtheray

Reputation: 21

In my case I was indeed able to resolve the issue by using John Smith's example without the brackets.

LibrariesSchema = new SimpleSchema({
  'books': {
      type: BooksSchema,
      minCount: 1
  }
});

Upvotes: 1

John Smith
John Smith

Reputation: 1579

LibrariesSchema = new SimpleSchema({
  'books': {
      type: [BooksSchema],
      minCount: 1
  }
});

Arrays of a specific types, for use in check() or schema definitions, are specified as [SomeType], eg. [String], or [BooksSchema] in your case.

Upvotes: 0

Related Questions