Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Separate form validation with Meteor

I'm using collection2 and I'm trying to get it to handle validation is a specific way. I have a profile schema which looks kind of like this:

Schema.UserProfile = new SimpleSchema({
    name: {
        type: String,
        optional: false
    }
    location: {
        type: String,
        optional: true
    }
    gender: {
        type: String,
        optional: false
    }
});

Schema.User = new SimpleSchema({
    username: {
        type: String,
        optional: true
    },
    emails: {
        type: Array,
        optional: true
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date
    },
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    roles: {
        type: [String],
        optional: true
    },
    heartbeat: {
        type: Date,
        optional: true
    }
});

Meteor.users.attachSchema(Schema.User);

Now, on my registration form I'm requiring the user to select their gender and then later once they log in, users are presented with a separate form asking for their name and location. Here's the problem:

The registration form works and everything goes through with saving. When they try to save the internal form with location and name though I get an error:

Error invoking Method 'updateProfile': Gender is required [400]

I know it's happening because it's required in the schema but I've already obtained this information. How do I not require that? Or do I set up validation per form?

like image 689
SeanWM Avatar asked Jan 24 '16 14:01

SeanWM


People also ask

How do you add validation to a form?

The simplest HTML validation feature is the required attribute. To make an input mandatory, add this attribute to the element. When this attribute is set, the element matches the :required UI pseudo-class and the form won't submit, displaying an error message on submission when the input is empty.

What is inline form validation?

In short, inline validation is an effort on behalf of the form to help users correct information as they go, ideally so that when the point comes to submit the form, they are more likely to submit it on the first attempt. The more failed attempts a user makes to submit the form, the more likely they are to give up.

Does HTML5 have form validation?

Validation attributesHTML5 added new attributes that declaratively set validation rules for a given input field. These new attributes include: required : Specifies that the field can't be blank. min / max : Specifies the range of allowed values for numeric inputs.


2 Answers

You have to add validation through jquery or you can use toaster for display the error on client side. Read this also : link

like image 145
Pankaj Jatav Avatar answered Oct 25 '22 09:10

Pankaj Jatav


I assume you use aldeed:autoform for your forms. When you use normal type in the form, all fields, even those already filled marked as mandatory have to be submitted. Two ways to fix this:

  • Dirty way: set hidden field with the prefilled value.
  • You can also set your form type as update as seen in the doc. This way, simple-schema will validate your newDoc already filled with your previous entries without screaming.

The solution number two is the one I use in most cases. This plus autoform's hooks give you enough flexibility to adapt to most use-cases you might encounter.

like image 3
ko0stik Avatar answered Oct 25 '22 10:10

ko0stik