Permalink
Browse files

Validate post contents when editing

chapter9-5
  • Loading branch information...
tmeasday committed Oct 15, 2014
1 parent 9791d73 commit b9f7042e891a7d7ea062da173349a119b96e76de
Showing with 29 additions and 3 deletions.
  1. +4 −2 client/templates/posts/post_edit.html
  2. +18 −1 client/templates/posts/post_edit.js
  3. +7 −0 lib/collections/posts.js
@@ -1,15 +1,17 @@
<template name="postEdit">
<form class="main form page">
<div class="form-group">
<div class="form-group {{errorClass 'url'}}">
<label class="control-label" for="url">URL</label>
<div class="controls">
<input name="url" id="url" type="text" value="{{url}}" placeholder="Your URL" class="form-control"/>
<span class="help-block">{{errorMessage 'url'}}</span>
</div>
</div>
<div class="form-group">
<div class="form-group {{errorClass 'title'}}">
<label class="control-label" for="title">Title</label>
<div class="controls">
<input name="title" id="title" type="text" value="{{title}}" placeholder="Name your post" class="form-control"/>
<span class="help-block">{{errorMessage 'title'}}</span>
</div>
</div>
<input type="submit" value="Submit" class="btn btn-primary submit"/>
@@ -1,3 +1,16 @@
Template.postEdit.onCreated(function() {
Session.set('postEditErrors', {});
});
Template.postEdit.helpers({
errorMessage: function(field) {
return Session.get('postEditErrors')[field];
},
errorClass: function (field) {
return !!Session.get('postEditErrors')[field] ? 'has-error' : '';
}
});
Template.postEdit.events({
'submit form': function(e) {
e.preventDefault();
@@ -9,6 +22,10 @@ Template.postEdit.events({
title: $(e.target).find('[name=title]').val()
}
var errors = validatePost(postProperties);
if (errors.title || errors.url)
return Session.set('postEditErrors', errors);
Posts.update(currentPostId, {$set: postProperties}, function(error) {
if (error) {
// display the error to the user
@@ -28,4 +45,4 @@ Template.postEdit.events({
Router.go('postsList');
}
}
});
});
View
@@ -12,6 +12,13 @@ Posts.deny({
}
});
Posts.deny({
update: function(userId, post, fieldNames, modifier) {
var errors = validatePost(modifier.$set);
return errors.title || errors.url;
}
});
validatePost = function (post) {
var errors = {};

0 comments on commit b9f7042

Please sign in to comment.