Sails.js pop-up message for user registration

I follow the Sail.js manual from http://irlnathan.imtqy.com/sailscasts/blog/2013/08/27/building-a-sails-application-ep4-handling-validation-errors-with-a-flash-message /

However, I ran into a little problem. In the textbook, the author uses the registration files in his user folder and assigns routes in the user controller. It then sends flash check errors to the user.

However, in my project, the log files are in the root folder, and I assign the routes from the routes.js file as follows

 module.exports.routes = { '/': { view: 'index' }, '/register': { view: 'register' } }; 

The problem now is using flash to show users validation errors during registration. I used the following in a user controller (create) and it does not work

 if (err){ err.success = 0; console.log(err); req.session.flash = { err: err } req.flash('error', req.session.flash); return res.redirect('/register'); } 

Any suggestions?

The version of Sails.js <0.10.x based on its other release is here

+6
source share
3 answers

EDIT: See the Sails documentation here for more information - basically, since you use static routing, the view policies are not applied before rendering, so the flash policy does not work. I would recommend adding a register action to your UserController and just call res.view() from there. There's also fooobar.com/questions/1182926 / ... which discusses this if you want more information.

I have an alternative that I developed for my own project, which you can try (also requires non-static routing).

In your api/policies folder, create a flash.js policy:

 // flash.js policy module.exports = function(req, res, next) { res.locals.messages = { success: [], error: [], warning: [] }; if(!req.session.messages) { req.session.messages = { success: [], error: [], warning: [] }; return next(); } res.locals.messages = _.clone(req.session.messages); // Clear flash req.session.messages = { success: [], error: [], warning: [] }; return next(); }; 

This policy allows you to use three different types of flash memory: success, warning, and errors. It will create an empty dictionary for each session and clear it when the page loads.

I created the FlashService.js service in api/services for lighter flash messages:

 // FlashService.js module.exports = { success: function(req, message) { req.session.messages['success'].push(message); }, warning: function(req, message) { req.session.messages['warning'].push(message); }, error: function(req, message) { req.session.messages['error'].push(message); } } 

Then, in the config/policies.js configuration file, be sure to assign a flash policy to the controller actions that you want to use with:

 // config/policies.js module.exports.policies = { '*': [true, 'flash'], 'UserController': { 'register': ['flash'], // any future actions that want flash }, 'AnotherController': { 'someAction': ['flash', 'somePolicy'], } } 

To execute a flash message in a register action, simply use FlashService.success(req, message) . Alternatively, you can use FlashService.error and FlashService.warning , depending on how your user interface style works and how much you want to distinguish your flash messages.

In your opinion, you can add something like this:

 <% if (messages && messages['error'].length > 0) { %> <div class="alert alert-danger"> <% messages['error'].forEach(function(message) { %> <%= message %> <br> <% }); %> </div> <br> <% } %> <% if (messages && messages['warning'].length > 0) { %> <div class="alert alert-warning"> <% messages['warning'].forEach(function(message) { %> <%= message %> <br> <% }); %> </div> <br> <% } %> <% if (messages && messages['success'].length > 0) { %> <div class="alert alert-success"> <% messages['success'].forEach(function(message) { %> <%= message %> <br> <% }); %> </div> <br> <% } %> 

Of course, you will have to change the div classes to all classes relevant to your user interface.

+15
source

In the usual mvc view, rails or flash manipulations cakephp is something like this: $ this-> Flash-> error ('Error message'); then display it in the element.

In sails, it should be simple.

In the controller: req.flash ('error', 'Occurred message');

In the field of view Partial: flash.ejs

 <% if (req.session.flash) { %> <% if (req.session.flash.success) { %> <div data-alert class="alert-box success radius"> <%= req.flash('success') %> <a href="#" class="close">&times;</a> </div> <% } %> <% if (req.session.flash.warning) { %> <div data-alert class="alert-box warning radius"> <%= req.flash('warning') %> <a href="#" class="close">&times;</a> </div> <% } %> <% if (req.session.flash.error) { %> <div data-alert class="alert-box alert round"> <%= req.flash('error') %> <a href="#" class="close">&times;</a> </div> <% } %> <% } %> 

In the layout:

 <%- partial('partials/flash') %> 

Handling the error message for each field should be part of the validation helper and HTML. (For example, for example: https://gist.github.com/mikermcneil/8366092 )

+3
source

You can add this export:

 module.exports = function flash(req, res, next) { if (!req.session.flash) { req.session.flash = {}; req.session.flash['success'] = []; req.session.flash['warning'] = []; } next(); }; 

and then in the view:

 <% if (req.session.flash['success'].length > 0) { %> <div class="alert alert-success"> <%= req.session.flash['success'].pop() %> </div> <% } %> <% if (req.session.flash['warning'].length > 0) { %> <div class="alert alert-warning"> <%= req.session.flash['warning'].pop() %> </div> <% } %> 

You can create messages in the controller as follows:

 req.session.flash['success'].push('Updated successfully'); 
+1
source

Source: https://habr.com/ru/post/1200536/


All Articles