Validate date values in Meteor AutoForm SimpleSchema

寵の児 提交于 2019-12-10 20:57:08

问题


I have the following Schema:

Dates.attachSchema(new SimpleSchema({
    description: {
        type: String,
        label: "Description",
        max: 50
    },
    start: {
        type: Date,
        autoform: {
            afFieldInput: {
                type: "bootstrap-datepicker"
            }
        }
    },
    end: {
        type: Date,
        autoform: {
            afFieldInput: {
                type: "bootstrap-datepicker"
            }
        }
    }
}));

How can I validate that the end date is not before start? I am using MomentJS to handle date types, however my main problem is how I can access other attributes in the custom function.

For instance:

end: {
   type: Date,
   autoform: {
       afFieldInput: {
           type: "bootstrap-datepicker"
       }
   },
   custom: function() {
       if (moment(this.value).isBefore(start)) return "badDate";
   }
}

How can I access start?

Furthermore, how can I validate if the start + end date combination is unique, meaning there is no document saved in my database which has the exact same start and end date?


回答1:


For the inter-field communication, you can do:

end: {
  type: Date,
  autoform: {
    afFieldInput: {
      type: "bootstrap-datepicker"
    }
  },
  custom: function() {
    // get a reference to the fields
    var start = this.field('start');
    var end = this;
    // Make sure the fields are set so that .value is not undefined
    if (start.isSet && end.isSet) {
      if (moment(end.value).isBefore(start.value)) return "badDate";
    }
  }
}

You should of course declare the badDate error first

SimpleSchema.messages({
  badDate: 'End date must be after the start date.',
  notDateCombinationUnique: 'The start/end date combination must be unique'
})

Regarding the uniqueness, first of all simple schema itself does not provide uniqueness check. You should add aldeed:collection2 for that.

Furthermore, collection2 is capable of checking only a single field uniqueness. To accomplish compound indexes, you should use the ensureIndex syntax

Dates._ensureIndex( { start: 1, end: 1 }, { unique: true } )

Even after this, you will not be able to see the error from this compound index on your form because autoform needs to be aware that such error is existing.

AutoForm.hooks({
  NewDatesForm: { // Use whatever name you have given your form
    before: {
      method: function(doc) {
        var form = this;
        // clear the error that gets added on the previous error so the form can proceed the second time
        form.removeStickyValidationError('start');
        return doc;
      }
    },
    onSuccess: function(operation, result, template) {
      if (result) {
        // do whatever you want if the form submission is successful;
      }
    },
    onError: function(operation, error) {
      var form = this;
      if (error) {

        if (error.reason && error.reason.indexOf('duplicate key error')) {
          // We add this error to the first field so it shows up there
          form.addStickyValidationError('start', 'notDateCombinationUnique'); // of course you have added this message to your definition earlier on
          AutoForm.validateField(form.formId, 'start');
        }

      }
    }
  }
});


来源:https://stackoverflow.com/questions/33065326/validate-date-values-in-meteor-autoform-simpleschema

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!