Skip to content
Permalink
Browse files

Added length validation to title,

Fixed display of errors to use a list
  • Loading branch information...
John Troxel
John Troxel committed Jul 17, 2014
1 parent 01016a3 commit 7d4032e6b00aba57a31822d5123a5cb2c1f45fbe
@@ -11,24 +11,24 @@ var mongoose = require('mongoose'),
* Get the error message from error object
*/
var getErrorMessage = function(err) {
var message = '';
var messages = [];

if (err.code) {
switch (err.code) {
case 11000:
case 11001:
message = 'Post already exists';
messages.push('Post already exists');
break;
default:
message = 'Something went wrong';
messages.push('Something went wrong');
}
} else {
for (var errName in err.errors) {
if (err.errors[errName].message) message = err.errors[errName].message;
if (err.errors[errName].message) messages.push(err.errors[errName].message);
}
}

return message;
return messages;
};

/**
@@ -41,7 +41,7 @@ exports.create = function(req, res) {
post.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
messages: getErrorMessage(err)
});
} else {
res.jsonp(post);
@@ -67,7 +67,7 @@ exports.update = function(req, res) {
post.save(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
messages: getErrorMessage(err)
});
} else {
res.jsonp(post);
@@ -84,7 +84,7 @@ exports.delete = function(req, res) {
post.remove(function(err) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
messages: getErrorMessage(err)
});
} else {
res.jsonp(post);
@@ -98,7 +98,7 @@ exports.delete = function(req, res) {
exports.list = function(req, res) { Post.find().sort('-created').populate('user', 'displayName').exec(function(err, posts) {
if (err) {
return res.send(400, {
message: getErrorMessage(err)
messages: getErrorMessage(err)
});
} else {
res.jsonp(posts);
@@ -4,33 +4,36 @@
* Module dependencies.
*/
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
Schema = mongoose.Schema;

/**
* Post Schema
*/
var PostSchema = new Schema({
title: {
type: String,
default: '',
required: 'Please fill Post title',
trim: true
},
title: {
type: String,
default: '',
required: 'Please fill Post title',
trim: true,
validate : [
function(v) { return v.length >= 4; },
'Title must be at least 5 letters']

},
// calling the body post_body because just body is a pretty common string
post_body: {
type: String,
default: '',
required: 'Please fill Post body',
trim: true
required: 'Please fill Post body'
},
created: {
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
type: Date,
default: Date.now
},
user: {
type: Schema.ObjectId,
ref: 'User'
}
});

mongoose.model('Post', PostSchema);
@@ -17,7 +17,7 @@ angular.module('posts').controller('PostsController', ['$scope', '$stateParams',
post.$save(function(response) {
$location.path('posts/' + response._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
$scope.errors = errorResponse.data.messages;
});

// Clear form fields
@@ -48,7 +48,7 @@ angular.module('posts').controller('PostsController', ['$scope', '$stateParams',
post.$update(function() {
$location.path('posts/' + post._id);
}, function(errorResponse) {
$scope.error = errorResponse.data.message;
$scope.errors = errorResponse.data.messages;
});
};

@@ -18,8 +18,12 @@ <h1>New Post</h1>
<div class="form-group">
<input type="submit" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
<div class="text-danger">
<ul>
<li ng-repeat="error in errors">
{{error}}
</li>
</ul>
</div>
</fieldset>
</form>
@@ -18,8 +18,12 @@ <h1>Edit Post</h1>
<div class="form-group">
<input type="submit" value="Update" class="btn btn-default">
</div>
<div data-ng-show="error" class="text-danger">
<strong data-ng-bind="error"></strong>
<div class="text-danger">
<ul>
<li ng-repeat="error in errors">
{{error}}
</li>
</ul>
</div>
</fieldset>
</form>

0 comments on commit 7d4032e

Please sign in to comment.
You can’t perform that action at this time.