S
S
Sergey2015-03-13 11:45:15
Angular
Sergey, 2015-03-13 11:45:15

How to properly validate angular + mongoose fields?

There is a model

var ArticleSchema = new Schema({
    created: {
        type: Date,
        default: Date.now
    },
    title: {
        ru: [{
                type: String,
                default: '',
                trim: true,
                required: 'Заголовок не может быть пустым'
            }],
        en: [{
                type: String,
                default: '',
                trim: true,
                required: 'Title cannot be blank'
            }]
    },
    content: {
        type: String,
        default: '',
        trim: true
    },
    user: {
        type: Schema.ObjectId,
        ref: 'User'
    }
});

Have a server controller
exports.create = function(req, res) {
  var article = new Article(req.body);
  article.user = req.user;
         console.dir(req.body);

  article.save(function(err) {
    if (err) {
      return res.status(400).send({
        message: errorHandler.getErrorMessage(err)
      });
    } else {
      res.json(article);
    }
  });
};

I have an angular controller
$scope.create = function() {
      var article = new Articles({
        title: this.title,
                                
        content: this.content
      });
      article.$save(function(response) {
        $location.path('articles/' + response._id);

        $scope.title.ru = '';
                                $scope.title.en = '';
        $scope.content = '';
      }, function(errorResponse) {
        $scope.error = errorResponse.data.message;
      });
    };

How to do validation? In particular, that the fields are required. Now saves even if all fields are empty

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladislav Lyskov, 2018-01-09
@Vlatqa

th

O
Odisseya, 2018-01-09
@Odisseya

You can make the dots the bottom background image and give the text a white background.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question