Validating form with SimpleSchema

Hello @all,

I want to make a simple input validation. If the user type a falsy value then the input field must be change the border color to red with the error text inside the input field.

I want to realize this with the SimpleSchema plugin. Up to now I defined a Schema and modified the Error text like this:

var Schemas = {};

SimpleSchema.messages({
    "minNumber width": "Breite min. 500mm",
    "maxNumber width": "Breite max. 4000mm
});

Schemas.Plate = new SimpleSchema({
    width: {
	type: Number,
	label: "Width",
	decimal: true,
	min: 500,
	max: 4000
    }

My question is, what is the best way to get the html input field id ("#width") and the error text from the Meteor.method to locate the input field and to paste the error text inside the input field.
The next one is how to get multiple error if I have more than one field like “width” and “height”?

Sorry for my bad english :smile:

Ah, you are lucky because I solved this one last week :stuck_out_tongue:

Meteor.call( MethodeName, parameter1, parameter2, ... , function(error, result) {
        if (error) {
            console.log(error.reason);
            //the text is in "error.reason"
            $('#width').val(error.reason);
        }else{
            //if you want to return a value from the Method, it will be in "result"
        }
    }
1 Like

Thank you karthons,

but I don’t want to write the “#width” by hand, I will get it from the error.

Basically, you want the error message to know where to put the error message ? Since a method call happens once you submit both, this is not strictly possible.

You have 2 options :

  • you make 2 methods for each input field (seems redudant)
  • you parse the error message

Another possibility would be to do a client-side validation error message (before you call the server method).

Ok, I think will take the second one, because from my point of view it is not necessary to write a client validation because my methods are running clientside (simulation) and serverside. Please correct if I am wrong.

All methods you call are on the server. Once you do a Meteor.call, you send data to the server. And then the server answers if necessary.

The advantage of making client side validation, is that you don’t need to communicate with the server, to inform the user that he made a wrong input. Depending on your application, this might be important or not.

Yes you are right, but if my method is not in the “Meteor.isServer” block, the it executes first on client in form of a simulation and then on the server. I think it calls latency compensation.

1 Like

Oh. Well thanks, you just made me realize that I did not make use of latency compensation.

I used to put my methods on /server, wondering why latency compensation does not work…

Edit : on a side note, latency compensation exists to make changes feel instant. If errors are handled after sending the data, it might not feel instant anymore.

OK, I will try it out, thank you very much