Meteor-Community-Packages / meteor-simple-schema

Meteor integration package for simpl-schema

Home Page:https://github.com/Meteor-Community-Packages/meteor-simple-schema

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[solved] custom function validation error

fly19890211 opened this issue · comments

commented

Hello, thanks for the package.

I currently have the a schema as following and use it in autoform for calling a method.

the file of schema

export default historyFormSchema = new SimpleSchema({
    historyType: {
        type: String,
        optional: false,
        label: '歷史資料種類',
        autoform: {
            label: '歷史資料種類',
            type: "select",
            options: function() {
                return [
                    { label: '電廠 歷史資料', value:'stationHistory' },
                    { label: '逆變器 歷史資料', value:'inverterHistory' }
                ];
            }
        }
    },
    station: {
        type: String,
        label: 'Select Station',
        autoform: {
            label: '電廠',
            type: "select",
            options: function() {
                return SunForever.models.Stations.find().map(function(obj) {
                    if (Match.test(obj._id, String)) {
                        return { label: obj.Name, value: obj._id };
                    } else if (Match.test(obj._id, Match.OneOf(Meteor.Collection.ObjectID, Mongo.ObjectID))) {
                        return { label: obj.Name, value: obj._id._str };
                    } else {
                        return
                    }
                });
            }
        }
    },
    inverter: {
        type: String,
        label: 'Select Inverter',
        optional: true,
        custom: function() {
            let historyType = this.field('historyType').value;
            if (historyType === 'inverterHistory' && !this.isSet && (!this.operator || (this.value === null || this.value === ""))) {
                return 'required'
            }
        },
        autoform: {
            label: "逆變器",
            omit: function(fieldNames) {
                var historyType = AutoForm.getFieldValue("historyType");
                if (historyType === 'stationHistory') {
                    return true;
                } else if (historyType === 'inverterHistory') {
                    return false;
                } else {
                    return false
                }
            },
            options: function() {
                var selectedStationID = AutoForm.getFieldValue("station");
                if (selectedStationID && selectedStationID.length == 24) {
                    selectedStationID = new Mongo.ObjectID(selectedStationID)
                }
                var station = SunForever.models.Stations.findOne({ _id: selectedStationID });
                if (station) {
                    return station.allInverters.map(function(obj) {
                        if (Match.test(obj._id, String)) {
                            return { label: obj.Brand + ' ' + obj.Model + ' ' + obj.ModbusID, value: obj._id };
                        } else if (Match.test(obj._id, Match.OneOf(Meteor.Collection.ObjectID, Mongo.ObjectID))) {
                            return { label: obj.Brand + ' ' + obj.Model + ' ' + obj.ModbusID, value: obj._id._str };
                        } else {
                            return { label: 'none', value: 'none' }
                        }
                    });
                } else {
                    return [];
                }
            }
        }

    },
    interval: {
        type: String,
        label: 'interval',
        allowedValues: [
            'day',
            'month',
            'year'
        ],
        autoform: {
            label: '資料區間',
            options: function() {
                return [
                    { label: '日', value: 'day'},
                    { label: '月', value: 'month'},
                    { label: '年', value: 'year'},
                ];
            }
        }
    },
    dateFrom: {
        type: Date,
        label: 'Date From'
    },
    dateTo: {
        type: Date,
        label: 'Date To'
        // TODO: validate if dateTo is equal or greater than dateFrom
    }
});

and the code of html is

<template name="History">
    <div class="container-fluid">

        <div class="row">
            <div class="col-md-6">

                <!-- {{#autoForm schema=historyFormSchema id="queryHistoryForm" type="method" meteormethod="history1"}} -->
                {{#autoForm schema=historyFormSchema id="queryHistoryForm"}}
                {{#each afFieldNames}}
                {{> afQuickField name=this.name options=afOptionsFromSchema}}
                {{/each}}
                <div>
                    <button type="submit" class="btn btn-primary">submit</button>
                    <button type="reset" class="btn btn-default">Reset</button>
                </div>
                {{/autoForm}}

            </div>
        </div>


        <div class="row">
            <div id="history_result">

            </div>
        </div>

    </div>
</template>

Template js is

/*****************************************************************************/
/* History: Event Handlers */
/*****************************************************************************/
Template.History.events({
    'submit #queryHistoryForm': function(event, template) {
        event.preventDefault();
        var instance = Template.instance();
        var form_values = AutoForm.getFormValues('queryHistoryForm');
        var query_values = form_values.insertDoc;
        query_values.dateFrom.setHours(0);
        query_values.dateTo.setHours(23);
        query_values.dateTo.setMinutes(59);
        query_values.dateTo.setSeconds(59);
        console.log(query_values);
        Meteor.call("history", query_values, function(error, result){
            if(error){
                console.log(error);
                setTimeout(function(){
                    toastr.options = {
                        "positionClass": "toast-top-center",
                        "closeButton": true,
                        "progressBar": true,
                        "showEasing": "swing",
                        "timeOut": "3000"
                    };
                    toastr.error(error.message);
                },1000);
            } else if (result.length === 0) {
                setTimeout(function(){
                    toastr.options = {
                        "positionClass": "toast-top-center",
                        "closeButton": true,
                        "progressBar": true,
                        "showEasing": "swing",
                        "timeOut": "2000"
                    };
                    toastr.warning('there is no records on the database');
                },2000);

            } else {
                console.log(result);
                var data = query_values;
                data.station = SunForever.models.Stations.findOne({ _id: data.station });

                data.results = result;
                Blaze.renderWithData(Template.InverterHistoryTable, data, instance.find('#history_result'));
            }
        });
    },
});

/*****************************************************************************/
/* History: Helpers */
/*****************************************************************************/
Template.History.helpers({
    historyFormSchema: function() {
        import historyFormSchema from './historyFormSchema.js'
        return historyFormSchema
    }
});

/*****************************************************************************/
/* History: Lifecycle Hooks */
/*****************************************************************************/

and i always get error when checking the schema on the method on server
Exception while invoking method 'history' Error: Match error: Match error: Select Inverter is required

Can anyone give me a hint?

commented

sorry for creating the issue due to my mistake of coding