simov / express-admin

MySQL, MariaDB, PostgreSQL, SQLite admin for Node.js
MIT License
1.17k stars 223 forks source link

Datavalidation in PreSave Event #96

Closed ashwanikumar04 closed 8 years ago

ashwanikumar04 commented 8 years ago

Hello: Thanks for this awesome module. I am trying to use this for one of my project. I want to run custom validation in presave event and if the validation is failed I want to stop the save and show user an error message.

args.error = {
                        "error":
                        "This is not active"
                    };

I tried setting error in Presave event but it did not work.

Can you suggest any way here. Thanks.

simov commented 8 years ago

You should return the error inside the callback, I think:

exports.preSave = function (req, res, args, next) {
    next(new Error('Something bad happened!'));
}
ashwanikumar04 commented 8 years ago

Thanks. I tried similar way. but this way the page is changed to the list of the items. I want the user to be on same page with error shown.

simov commented 8 years ago

Just by looking at the code I don't think that's possible.

ashwanikumar04 commented 8 years ago

I managed to keep the user on the same page but no error is shown. I am using following code

console.log("This is not active");
                    req.session.error = "This is not active";
                    res.redirect(res.locals.root + '/'
                        + args.slug
                        + '/'
                        + pk);
                    return;

Still not able to show the error. Any idea?

simov commented 8 years ago

That's because the error message inside the editview is not dynamic https://github.com/simov/express-admin/blob/master/views/editview.html#L14

See how this is implemented in the listview for example https://github.com/simov/express-admin/blob/master/views/listview.html#L23