javascript - Show inline field validation errors along with email sent message to an end user filling up a form -
i'm working on app being developed on nodejs, expressjs, mongoose stack. i'm building form on submission of if there validation errors[ want use mongoose validation, keep validation code @ 1 place] re-render these error messages form , if fields valid send email end user if email not available in mongoose.
so form usual form except i'm doing rendering form expressjs , want enduser should info there field errors send email , notify end user @ same time email in process of being sent, , if email exists notify user instantly on form.
some psuedocode can me think in nodejs way appreciated. i'm coming python/django stack.
below email sender module returns promise object.
mailer.js
var path = require('path') , templatesdir = path.resolve(__dirname, '..', 'email_templates') , emailtemplates = require('email-templates') , nodemailer = require('nodemailer') , rsvp = require('rsvp') ; var sendemail = function (template_name, locals) { return new rsvp.promise(function(resolve,reject){ emailtemplates(templatesdir, function(err, template) { if (err) { return reject({error: err}) } else { emailfrom = 'an_email_address', mailer = { host: 'some_amazon_instance', port: 25, service: 'smtp', // gmail, smtp, zoho auth: { user: 'credential', pass: 'credential' } }; var transport = nodemailer.createtransport("smtp", mailer); var emailto = emailfrom; if (!locals.admin) { emailto = locals.email; } // send single email template(template_name, locals, function(err, html, text) { if (err) { } else { transport.sendmail({ from: emailfrom, to: emailto, subject: locals.title, html: html, text: text }, function(err, responsestatus) { if (err) { transport.close(); return reject({error: err}); } else { resolve({result: responsestatus.message}); } transport.close(); }); } }); } }); }); } exports.sendemail = sendemail;
below mongoose model handle form data, validation , email sending.
invite.js
'use strict'; /** * module dependencies. */ var mongoose = require('mongoose') , schema = mongoose.schema , sendemail = require('meanio').loadconfig().sendemail , rsvp = require('rsvp') ; var studioinviteschema = new schema({ name: { type: string, required: [true, 'name required'] }, email: { type: string, index: true, unique: true, required: [true, 'email required'] }, phone: { type: number, index: true, unique: true, required: [true, 'phone required'] }, address: { type: string, required: [true, 'address required'] }, email_template: {type: string, required: true}, title: {type: string, require: true} }); studioinviteschema.path('phone').validate(function (value) { return parseint(value)!=nan && value.tostring().length == 10; }, 'enter valid phone number'); studioinviteschema.path('email').validate(function (value) { var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)| (\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-za-z\-0-9]+\.)+[a-za-z]{2,}))$/; return re.test(value) }, 'enter valid email'); studioinviteschema.methods = { saveandemailtoadmin: function () { var instance = this; console.log(instance, this, 42); return new rsvp.promise(function (resolve, reject) { instance.save(function (err) { if (err) { if (err.code == 11000) { return reject({message: 'duplicate invite'}) } return reject({message: err}) } var admin_email_context = { name: instance.name, email: instance.email, phone: instance.phone, address: instance.address, title: instance.title, admin: true } var studio_email_context = { name: instance.name, message: "we'll contact shortly", email: instance.email, title: 'invite received', admin: false } var admin_email = sendemail(instance.email_template, admin_email_context) var studio_email = sendemail('userinvite', studio_email_context) rsvp.allsettled([admin_email, studio_email]) .then(function (array) { console.log(array) resolve({message: 'success'}) }, function (error) { resolve({message: 'error'}) }) }) }); } }; mongoose.model('studioinvite', studioinviteschema);
and below controller of html view invoke saveandemailtoadmin
of invite.js model form validation , email sending.
invite_controller.js
exports.studioinvite = function (req, res) { var invite = new studioinvite({ name: req.body.name, email: req.body.email, phone: req.body.phone, address: req.body.address, email_template: 'studioinvite', title: 'invite request studio' }); /* todo: have use notifications send messages end users invite sent or there duplicacy */ invite.saveandemailtoadmin() .then(function (result) { console.log(result) return [ send success messages ] }, function (error) { console.log(error); return [ send error messages] }); return [ dont want return @ line if email sending takes time have return response ] }
i've found can use blocking code means i've put user on hold untill done on server, can use socket.io send realtime notifications end users validation messages , email sent notifications. solution doesn't seem me.
Comments
Post a Comment