gpt4 book ai didi

javascript - Keystone JS 中的验证错误

转载 作者:搜寻专家 更新时间:2023-10-31 23:17:42 26 4
gpt4 key购买 nike

我正在尝试构建一个与 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'])

关于javascript - Keystone JS 中的验证错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28333347/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com