Search code examples
validationember.jsember-datasails.jsdatastore

How do I validate data on the backend which is pass in from Ember Data


For the past couple of days I've been playing with Ember CLI, using the SANE stack. After getting everything working and making a todo application like the one from the todo MVC website (todomvc.com), I'm now trying to validate data on my server (a sails js server).

For example, when a new todo is saved ('Do shopping on Tuesday'), Ember saves the new record in the todo model in the Ember data store. Ember data then updates the todo model back in the sails server, by sending a POST request to 'http://localhost:4000/api/v1/todos'. How do I tell Ember data that it needs to call a controller on the backend like 'http://localhost:4000/api/v1/todos/addTodo', instead of directly inserting the data into the backend's model?

Maybe it's me but does Ember data only talk to a backend's model. If so, how on earth do you validate data past into the server from the client side. I can only see validation being performed on the client side through the controllers, which is always going to be insecure. I just want to validate data on the backend...


Solution

  • Okay. Kinda figured it out. You have to configure sails js's routing by setting up sails to run certain actions from a controller whenever the user sends a GET or POST request to a certain URL.

    For example, this piece of code: 'post /api/v1/todos': { controller: 'Todo', action: 'addTodo' } - is in my sails js router file. Now every time a user makes a POST request to 'localhost:4200/api/v1/todos', the request will be passed into the addTodo controller, where I can now validate the request parameters and send a response back to ember js.

    I'm now gonna follow this tutorial on how to send error messages to ember js from a backend. http://yoranbrondsema.com/displaying-error-messages-ember-js-forms/.