Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I validate a form being filled out in Backbone?
    primarykey
    data
    text
    <p>So I have a new form set up it saves temporarily and all but I want it to only be able to update when it is validated otherwise show some errors. This is during the view section for the saveEdits event Any clue as to what I am doing wrong?</p> <p>This is my main.js file</p> <pre><code>(function () { window.App = { Models: {}, Collections: {}, Views: {}, Templates: {}, Router: {} }; // MODEL App.Models.User = Backbone.Model.extend({ defaults: { firstName: 'first', lastName: 'last', email: 'Email', phone: '222', birthday: '07/22/1980' }, validate: function (attrs) { if (!attrs.firstName) { return 'You must enter a real first name.'; } if (!attrs.lastName) { return 'You must enter a real last name.'; } if (attrs.email.length &lt; 5) { return 'You must enter a real email.'; } if (attrs.phone.length &lt; 10 &amp;&amp; attrs.phone === int) { return 'You must enter a real phone number, if you did please remove the dash and spaces.'; } if (attrs.city.length &lt; 2) { return 'You must enter a real city.'; } }, initialize: function() { this.on('invalid', function (model, invalid) { console.log(invalid); }); } }); //var userModel = new App.Models.User(); //VIEW App.Views.User = Backbone.View.extend({ el: '#user', //model: userModel, //tagName: 'div', //id: 'user', //className: 'userProfile', //template: _.template($("#userTemplate").html()), //editTemplate: _.template($("#userEditTemplate").html()), initialize: function (){ }, render: function() { this.template = Handlebars.compile($("#userTemplate").html()); this.editTemplate = Handlebars.compile($("#userEditTemplate").html()); this.$el.html(this.template(this.model.toJSON())); return this; }, events: { 'click button.edit': 'editProfile', 'click button.save': 'saveEdits', 'click button.cancel': 'cancelEdits' }, editProfile: function () { this.$el.html(this.editTemplate(this.model.toJSON())); }, saveEdits: function () { var form = $(this.el).find('form#updateUser'); this.model.set({ firstName : form.find('.firstName').val(), lastName : form.find('.lastName').val(), email: form.find('.email').val(), phone: form.find('.phone').val(), birthday: form.find('.birthday').val() }); this.model.validate(); this.render(); }, cancelEdits: function() { this.render(); } }); //start history service Backbone.history.start(); var user = new App.Views.User({model: new App.Models.User()}); user.render(); })(); </code></pre> <p>It works fine until I insert the this.model.validate and an error shows up stating this: <code>Uncaught TypeError: Cannot read property 'firstName' of undefined</code> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload