Keystone JS 中的验证错误

Validation error in Keystone JS

我正在尝试构建一个与 keystone 演示中使用的非常相似的联系表单,但我遇到了障碍,在尝试保存到数据库时,出现以下错误

    { message: 'Validation failed',
  name: 'ValidationError',
  errors: 
   { name: 
      { name: 'ValidatorError',
        path: 'name',
        message: 'Name is required',
        type: 'required' } } }

我已经通过 console.log 检查了表单上的字段以及后端中的请求,但由于某种原因,我仍然不断收到相同的错误。

这是我的 jade 文件中的内容

section#contact-container
   section#contact.contact-us
    .container
      .section-header
        // SECTION TITLE
        h2.white-text Get in touch
        // SHORT DESCRIPTION ABOUT THE SECTION
        h6.white-text
          | Have any question? Drop us a message. We will get back to you in 24 hours.

      if enquirySubmitted
       .row
          h3.white-text.wow.fadeInLeft.animated(data-wow-offset='30', data-wow-duration='1.5s', data-wow-delay='0.15s') Thanks for getting in touch.
      else
        .row
          form#contact.contact-form(method="post")
             input(type='hidden', name='action', value='contact')
             .wow.fadeInLeft.animated(data-wow-offset='30', data-wow-duration='1.5s', data-wow-delay='0.15s')
              .col-lg-4.col-sm-4(class=validationErrors.name ? 'has-error' : null)
                input.form-control.input-box(type='text', name='name', value=formData.name, placeholder='Your Name')
              .col-lg-4.col-sm-4
                input.form-control.input-box(type='email', name='email', value=formData.email, placeholder='Your Email')
              .col-lg-4.col-sm-4
                div(class=validationErrors.enquiryType ? 'has-error' : null)
                input.form-control.input-box(type='text', name='enquiryType', placeholder='Subject', value=formData.enquiryType)
             .col-md-12(class=validationErrors.message ? 'has-error' : null)
             .col-md-12.wow.fadeInRight.animated(data-wow-offset='30', data-wow-duration='1.5s', data-wow-delay='0.15s')
               textarea.form-control.textarea-box(name='message', placeholder='Your Message')= formData.message
             button.btn.btn-primary.custom-button.red-btn.wow.fadeInLeft.animated(data-wow-offset='30', data-wow-duration='1.5s', data-wow-delay='0.15s', type='submit') Send Message

这就是我的架构和路由文件的样子

    var keystone = require('keystone'),
    Types = keystone.Field.Types;

var Enquiry = new keystone.List('Enquiry', {
    nocreate: true,
    noedit: true
});

Enquiry.add({
    name: { type: Types.Name, required: true },
    email: { type: Types.Email, required: true },
    enquiryType: { type: String },
    message: { type: Types.Markdown, required: true },
    createdAt: { type: Date, default: Date.now }
});

Enquiry.schema.pre('save', function(next) {
    this.wasNew = this.isNew;
    next();
});

Enquiry.schema.post('save', function() {
    if (this.wasNew) {
        this.sendNotificationEmail();
    }
});

Enquiry.schema.methods.sendNotificationEmail = function(callback) {

    var enqiury = this;

    keystone.list('User').model.find().where('isAdmin', true).exec(function(err, admins) {

        if (err) return callback(err);

        new keystone.Email('enquiry-notification').send({
            to: admins,
            from: {
                name: 'Wheatcroft Accounting',
                email: 'contact@abc.com'
            },
            subject: 'New Enquiry for **',
            enquiry: enqiury
        }, callback);

    });

};

Enquiry.defaultSort = '-createdAt';
Enquiry.defaultColumns = 'name, email, enquiryType, createdAt';
Enquiry.register();

这是路由文件

  var keystone = require('keystone'),
     async = require('async'),
     Enquiry = keystone.list('Enquiry');


 exports = module.exports = function(req, res) {

     var view = new keystone.View(req, res),
         locals = res.locals;

     locals.section = 'contact';
     locals.formData = req.body || {};
     locals.validationErrors = {};
     locals.enquirySubmitted = false;

     view.on('post', { action: 'contact' }, function(next) {
         var newEnquiry = new Enquiry.model();
         var updater = newEnquiry.getUpdateHandler(req);
         updater.process(req.body, {
             flashErrors: true,
             fields: 'name, email, enquiryType, message',
             errorMessage: 'There was a problem submitting your enquiry:'
         }, function(err) {
             if (err) {
                 locals.validationErrors = err.errors;
                 console.log(err);
             } else {
                 locals.enquirySubmitted = true;
             }
             next();
         });

     });

     view.render('contact');

 }

我认为问题与 KeystoneJS 在内部处理 Types.Name 字段类型的方式有关。

在您的 jade 文件中,您应该使用指向其虚拟 name.full 属性 的路径来引用您的 name 字段。在内部 name.full 有一个 setter 将 name 分成 name.firstname.last。因此,如果您希望将名字和姓氏分开 input,您应该使用 name.firstname.last。如果您想要单个 input 输入全名,您应该使用 name.full.

尝试替换您的姓名字段的 input 控件:

input.form-control.input-box(type='text', name='name', value=formData.name, placeholder='Your Name')

有了这个:

input.form-control.input-box(type='text', name='name.full', value=formData['name.full'])