验证提交的表单并显示错误优雅

问题描述 投票:0回答:1

我想验证用户提交的表单和显示有意义的错误,但我有很多的问题。首先,我的表格(event_form.ejs):

<% if (typeof errors !== 'undefined') { %>
    <div class="message"><%= errors %></div>
<% } %> 

<form method="POST" action="/submit-create-event" id="create-event">
  <input type="text" class="title" name="title" placeholder="title">
  <input type="text" class="date" autocomplete="off" name="date" placeholder="date">
  <select class="type" name="type">
    <option value="road">Road</option>
    <option value="mtb">MTB</option>
  </select>
  <input type="text" class="city" name="city" placeholder="city">
  <select class="level" name="level">
      <option value="beginner">Beginner</option>
      <option value="advanced">Advanced</option>
      <option value="pro">Pro</option>
  </select>
  <input type="text" class="start-time timepicker" autocomplete="off" data-time-format="H:i" name="startTime" placeholder="start time">
  <input type="text" class="start_location" name="startLocation" placeholder="start location">
  <input type="text" class="distance" name="distance" placeholder="distance">
  <input type="submit" class="create-event-submit">
</form>

在提交时,在我的eventController.js我有函数来处理提交。

//Helper function to handle event form submit.
module.exports.submitEventForm = function(req, res) {

  const event = new eventModel.event({
    title: req.body.title,
    date: req.body.date,
    type: req.body.type,
    city: req.body.city,
    level: req.body.level,
    start_time: req.body.startTime,
    start_location: req.body.startLocation,
    distance: req.body.distance
  });

  event.save(function(err, data) {
    if (err) {
      console.log(err);
      //I tried this, kinda works but pretty sure a bad practice
      res.render('forms/create_event_form', {errors: err})
    } else {
      res.render('status/success');
    }
  });
};

正如你所看到的,在event.save()如果我有错误,我console.log()他们的时刻。我试图做再次渲染我event creation形式和显示错误(如我.ejs文件可见)。敢肯定这应该不是这样做。

这是一个modelvalidator文件

var mongoose = require('mongoose')
var validate = require('mongoose-validator')
var Schema = mongoose.Schema;


var titleValidator = [
  validate({
    validator: 'isAlphanumeric',
    message: 'Title should be alphanumeric'
  })
]

var eventSchema = new Schema({
  title: {type: String, required: true, validate: titleValidator},
  date: {type: String },
  type: {type: String },
  city: {type: String },
  level: {type: String },
  start_time: {type: String },
  start_location: {type: String },
  distance: {type: Number },
});

var Event = mongoose.model('Event', eventSchema);

module.exports.eventSchema = eventSchema;
module.exports.event = Event;

我创建titleValidator,如果我的方式失败时,它打印出这样的:

Error message on incorrect tilte

我的问题是:如何能够正确地把我的validator消息模板,并显示呢?如何处理多个消息再次validators的创建,为错误不是在array地回来了,这意味着我可以不通过这些循环?

express mongoose ejs mongoose-schema
1个回答
0
投票

有点找到了解决办法。为了使错误消息的更多可读和可使用的,我用Mongoose error helper。它整齐地打印出所有的错误信息。然而,通过npm让包时,有在代码中的错误和pull request是一个解决方案创建的,但是从来没有合并。只需手动做出很小的变化。

我找不到渲染出错的形式相同的另一种方式,所以我贴这个:

event.save(function(err) {
    if (err) {
      res.render('forms/create_event_form', {errors: errorHelper(err, next)});
    } else {
      res.render('status/success');
    }
  }); 

在我.ejs模板打印出的错误如下:

<% if (typeof errors !== 'undefined') { 
   errors.forEach(function(err) { %>
    <div class="message"><%= err %></div>
<% })} %> 

希望这可以帮助别人,欢呼声。

© www.soinside.com 2019 - 2024. All rights reserved.