Search code examples
node.jsvalidationparsleymulti-step

How do I set up parsely with nodejs?


I've been working on form validation for days. I'm trying to validate a multi step form.

I'm using Nodejs, ES6, Express, PUG. I'm having a hard time finding something that works. I think parsley would do it for me, but I cant get it to work with NodeJS.

For the code in my .js file (Where risk-calc is the id of my form):

$('.form-navigation .next').click(function() {
  if ($('#risk-calc').parsley().validate({group: 'block-' + curIndex()}))
    navigateTo(curIndex() + 1);

My console error is '$(...).parsley is not a function.'

I've installed via NPM parselyjs and jquery.

edit.

Attempting to use webpack to add global variables.

Something is still wrong, but how does this look?

plugins: [
    new ExtractTextPlugin('style.css'),
    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery',
      parsley: 'parsleyjs/dist/parsley.js'
    }),

Solution

  • You will need to include the parsely javascript code after you include the jquery code. That will then extend the jquery object to have the parsely method.

    If you have imported both through npm then you will need import the parsleyjs module at the top of your main entry file for you javascript before bundling it. I would recommend using webpack for the bundling.

    import 'parsleyjs'
    

    Usually jQuery can be added to your project files globally through the webpack provide plugin.

    See: https://webpack.js.org/plugins/provide-plugin/

    new webpack.ProvidePlugin({
      $: 'jquery',
      jQuery: 'jquery'
    })