2
votes

I am playing around with Meteor and autoform.

My setup

I have an input field (type "time") as part of my schema for the collection of "Songs" that forces the user to type the syntax [num][num]:[num][num]. This is my schema:

time: {
    type: Number,
    label: "Time",
    optional: true,
    autoform: {
        afFieldInput: {
            type: 'time'
        }
    }
}

What I want to do

After hitting "submit" but before validation I want to convert the string (e.g. "03:45") to seconds (Number) so that the validation passes without an error.

Also: when reading the data from the db I want to convert it back to a string, so that it fits in the input field as a value.

I could not find the answer in the docs for autoform, collection2 or simple-schema (or at least did not understand it ;-)

Thanks for your help!

1
I managed to convert the string to seconds by defining a autoValue: function() {}in the Sings schema. I did not try the way back from the db to the form yet. Another thing is, that I need the ID of the album as an attribute for the song-doc. Without autoform I had passed it from the template helper as an parameter to the Meteor.call(), but with autoform I don't know the best way to do it. Maybe via hidden input field? Or any more fancy way?CeeJay84

1 Answers

0
votes

Use autoform hooks. The information that you are looking for is here: https://github.com/aldeed/meteor-autoform#callbackshooks

More specifically what you are looking for is this:

AutoForm.hooks({
  someFormId: {
    formToDoc: function(doc) {
        // Called every time an insert or typeless form
        // is revalidated, which can be often if keyup
        // validation is used.
    },
    docToForm: function(doc, ss) {
        // Called whenever `doc` attribute reactively changes, before values
        // are set in the form fields.
    }
});

The doc can be modified in each hook specifically when it is going to the collection and when it is being reactively pulled from the collection.