guillaumepotier / Parsley.js

Validate your forms, frontend, without writing a single line of javascript
http://parsleyjs.org
MIT License
9.04k stars 1.31k forks source link

AMD loader does not work #693

Closed foxx closed 10 years ago

foxx commented 10 years ago

There is a problem with the AMD approach used


  // AMD Compliance
  if ( "function" === typeof define && define.amd ) {
    define( 'validator', [],function() { return exports; } );
  }
} )( 'undefined' === typeof exports ? this[ 'undefined' !== typeof validatorjs_ns ? validatorjs_ns : 'Validator' ] = {} : exports );

  var ParsleyValidator = function (validators, catalog) {
    this.__class__ = 'ParsleyValidator';
    this.Validator = Validator;

Notice how ParsleyValidator uses global ref Validator, however in the definition above Validator is only written to this if exports is undefined. For AMD, this will never be so, and as such the Validator module goes into exports.

This resolves in the following failure;

Uncaught ReferenceError: Validator is not defined

The most immediate fix seems to use something like;

this.Validator = exports.Validator;

However this has not been fully tested and would advice one of the core devs look at this.

guillaumepotier commented 10 years ago

I've updated parsley to fix this behavior, could you have a try with latest master version?

Thanks

iongion commented 10 years ago

The issue is still present in 2.0.4

Validator = 'undefined' !== typeof Validator ? Validator : module.exports;

shouldn't you also check for module (strict settings) ?

Validator = 'undefined' !== typeof Validator ? Validator : ('undefined' !== typeof module ? module.exports : null);
perfusorius commented 10 years ago

I am getting this error too when using 2.0.4 with RequireJS. 2.0.3 was fine.

ReferenceError: module is not defined
--> Validator = 'undefined' !== typeof Validator ? Validator : module.exports;
http://.../components/parsleyjs/dist/parsley.js, Line 866

Suggestion by @iongion also doesn't work:

TypeError: Validator is null
--> return $.extend(new Validator.Assert().Required(), { priority: 512 });
http://.../components/parsleyjs/dist/parsley.js, Line 949
jonmiles commented 10 years ago

I'm also running into this, or very similar issue with AMD loader... I'm getting this error when running 2.0.4.

Uncaught ReferenceError: module is not defined 

thrown from parsley.js 866

 Validator = 'undefined' !== typeof Validator ? Validator : module.exports;

Also tried @iongion suggestion and then I get the same error as @perfusorius.

Uncaught TypeError: Cannot read property 'Assert' of null parsley.js:949
  ParsleyValidator.validators.required parsley.js:949
  getPriority parsley.js:1495
  ConstraintFactory parsley.js:1497
  ParsleyField.addConstraint parsley.js:1602
  ParsleyField._bindConstraints parsley.js:1637
  ParsleyField parsley.js:1529
  Parsley.bind parsley.js:1982
  Parsley.init parsley.js:1899
  Parsley parsley.js:1876
  $.fn.parsley.$.fn.psly
superelement commented 10 years ago

Was having the same problem on a new RequireJS project. I've applied a fix for this on a fork here: https://github.com/jimdoyle82/Parsley.js.git Wasn't sure how to run the tests though so haven't done a pull request yet and won't have time for a few days. Feel free to clone the fork though in the meantime. Problem was that the Validator object (in Validator.js) wasn't defined on the global namespace once RequireJS dependency as removed in the Grunt build. So I modified the Grunt build to add a global variable called "Validator" and made the self-executing wrapper function return an object with Validator and Assert properties. Everything seems to be working well now.

guillaumepotier commented 10 years ago

Hi @jimdoyle82, your code looks great ! Could you make a PR, I'll test it and finish it if you don't have time.

Best

superelement commented 10 years ago

Ok, that's done. Over to you.

guillaumepotier commented 10 years ago

This issue should have been fixed with 2.0.5.

Feel free to reopen if not the case.

Best

jonmiles commented 10 years ago

Thanks @guillaumepotier @jimdoyle82

I can confirm release 2.0.5 works in my require.js setup, and resolves the issue I had reported. Great work, thanks again!