guillaumepotier / Parsley.js

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

parsley().refresh() does not reload the form if attributes are added dynamically after page load. #1336

Closed rob101 closed 3 years ago

rob101 commented 3 years ago

What kind of issue is this? (put 'x' between the square brackets)

Problem When changing field attributes dynamically, parsley().refresh() does not reload the form and bind parsley field attributes.

Example

Consider the codepen here: https://codepen.io/alias51/pen/rNxVQXw.

ParsleyJS is loaded via the data-parsley-validate attribute of the form $('#parsley_form') on page load, however no inputs on the form have any validators set at this stage.

If data-parsley-required attribute is added to a field dynamically (by an onclick event listener for $('#enable'), then it will validate and throw and error on form submit.

If we call $('#form_parsley').parsley().refresh() on the form before submit, then the expected behaviour would be that the field is bound to the Parsley instance and the change trigger should apply as if the data-parsley-required attribute was present at page load. However, it does not: for example, click "Enable Parsley" and then add input to the field, remove it, and change the focus. No error is thrown.

parsley().refresh() does not behave as expected.

marcandre commented 3 years ago

Awesome report! I'll try to have a better look at it later today, but I see a bunch of typos "parsely <=> parsley" (I do the same all the time!).

rob101 commented 3 years ago

@marcandre Yes, embarrassing! These were in the variable name declarations though, so doesn't affect the logic. I've corrected the code and the error still reproduces. Thanks

marcandre commented 3 years ago

The HTML still had one typo.

The global config setting was the issue. No need for the refresh, parsley automatically rechecks the constraints upon validation. Here's a Codepen that shows it in action: https://codepen.io/marcandre/pen/yLeYBZw

rob101 commented 3 years ago

Thanks. The annotated source (link) has:

window.ParsleyConfig = Parsley.options; // Old way of accessing global options

But there doesn't seem to be a reference to the correct way to set options the "new" way. window.Parsley.options = {} doesn't seem to work. How do you set trigger in a dict with other options like errorClass, errorsWrapper, etc?

marcandre commented 3 years ago

Right, you can't replace the object used, just modify it. Use $.extend :-)