Using quickForm with a collection and separate schema at the same time does not seem to work

309 views Asked by At

I want to store a time value in seconds in the DB.

In the form, the user should be able to type it as a String (MM:SS). After submission the String (MM:SS) should be transformed into seconds. That is why the schema the form is validated against differs from the schema used to validate against in the backend (right before writing it to the database).

So I did what is supposed here (https://github.com/aldeed/meteor-autoform#autoform-1) and I defined two schemas, one for the form (with time.type = "String") and another one that I attached to the collection (time.type = Number).

In the template I set both parameters, collection="TimeItem" and schema="SpecialFormSchema.

In the end, the form always renders with a HTML Number input field and ignores the form schema.

Can anybody help? Thanks in advance!

2

There are 2 answers

0
Colja On BEST ANSWER

It actually works as it should. My silly mistake was to experiment with different templates and worked on the wrong one and therefore did not see any results.

The working javascript:

// schema for collection
var schema = {
    time: {
        label: "Time (MM:SS)",
        type: Number // !!!
    },
    // ...
};
SongsSchema = new SimpleSchema(schema);
Songs.attachSchema(SongsSchema);

// schema for form validation
schema.time.type = String // changing from Number to String!
SongsSchemaForm = new SimpleSchema(schema);

What the template looks like:

{{>quickForm
   id="..."
   type="insert"
   collection="Songs"
   schema="SongsSchemaForm"
}}
8
Elie Steinbock On

The collection and schema are not supported together. You have to pick one or the other.

Try using hooks to do what you're trying to do: https://github.com/aldeed/meteor-autoform#callbackshooks