4

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

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

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

这是我的玉文件中的内容

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');

 }
4

1 回答 1

3

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

在您的文件中,您应该使用指向其虚拟属性的路径来jade引用您的字段。内部有一个 setter 将into和. 因此,如果您希望将名字和姓氏分开,您应该使用and 。如果你想单输入你应该使用的全名。namename.fullname.fullnamename.firstname.lastinputname.firstname.lastinputname.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'])
于 2015-02-04T23:44:08.257 回答