0

I'm writing an Appliction using Meteor. In this App I want to implement a server-side validation of the user data using Accounts.onCreateUser. There is some data passed which can only be verified on the server side.

At client side I call:

Template.register.events({
    'submit form': function (e) {
        e.preventDefault();
        var attributes = {
            username: $("#inputUsername").val(),
            password: $("#inputPassword").val(),
            confirmation: $("inputConfirmation").val(),
            email: $("#inputEmail").val(),
            ...
        };

        Accounts.createUser(attributes, function(err){
            if (err) {
                throwError(err);
            } else {
            }
        });
    }
});

And on the server side:

Accounts.onCreateUser(function(options, user) {
    if(!verifyData(options))
        throw new Meteor.Error(403, "Wrong input");
    return user;
});

After the server side verification fails, all form data is lost. What is the best way to keep the data?

MarcS82
  • 2,065
  • 7
  • 29
  • 46
  • What is the reason why you implemented server side validation ? If you do nothing that is not possible on the client I would strongly advise to just validate on the client-side. Could you maybe also elaborate more on what it is that you want to do with the data once the validation doesn't pass? – JKaan Dec 17 '14 at 19:27
  • An example could be a captcha, which has to be verified by the server or data which has to be checked against the database. When the validation don't pass the values of the input fields should be still there. – MarcS82 Dec 17 '14 at 20:58
  • What is happening inside your `throwError` function? – sbking Dec 18 '14 at 00:04
  • @Kishin everything needs to be validated on the server side... Otherwise a user could open their browser console and send bad data to the server directly with a method call. – sbking Dec 18 '14 at 00:06
  • throwError only inserts the error message into an Meteor.Collection – MarcS82 Dec 18 '14 at 14:48

1 Answers1

1

I went ahead and reproduced your code on a Meteorpad and from what I can tell, the form data does still persist. You just need to access it via the attributes variable in the client-side.

There may be something I am missing, but i took what you posted above and put it in there.

rgoomar
  • 184
  • 1
  • 3
  • 2
    Thank. Your code on Meteorpad shows that everthing works as expected. I did a mistake using the iron:router which caused a reload. – MarcS82 Dec 19 '14 at 11:35