Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Circular references makes it impossible to serialize ParsleyForm #1036

Closed
kevin-brown opened this issue Jan 3, 2016 · 4 comments
Closed

Comments

@kevin-brown
Copy link

Someone is going to look at the title of this and think: Why on earth would anyone want to serialize Parsley objects?

And that's where Select2 comes in. Specifically, the following ticket: select2/select2#4014 (comment)

Select2 uses jQuery.extend to clone the .data() attached to an element. Because it recursively copies the data, this causes recursion issues when trying to clone the ParsleyField attached to the element. You can see this at the following jsfiddle: http://jsfiddle.net/jew1mg89/8/

This is because ParsleyField.parent points to ParsleyForm. And ParsleyForm.fields points to instances of ParsleyField. I can't figure out why, because ParsleyField.parent is never actually used within ParsleyField, and it even appears to be an optional attribute that won't always be present. It looks like it was introduced in 740ed31, and it might just be that the functionality that used it has since been removed.

@marcandre
Copy link
Collaborator

Actually, parent is used. If you call $('some-input').parsley().validate(), that input must be able to actualize the options of the parent form. See the code in ParsleyAbstract's actualizeOptions.

I'm not sure if there's anything simple we can do to simplify things for select2. My first opinion is that we should be free to store whatever we please in our own data, and that it's a pity that jquery doesn't have a safe deep clone.

I'll comment further on select2's issue.

@kevin-brown
Copy link
Author

I'm going to take a shot at avoiding cross-repository references, to centralize discussion.

I see that ParsleyAbstract uses parent to get access to the form which the field is holding, which makes sense.

Is there any time when the ParsleyForm for the input won't be attached to the <form> holding it? Because it looks like $('#input').closest('form').data('Parsley') is the same as $('#form').data('Parsley').

http://jsfiddle.net/jew1mg89/10/

I'm definitely not familiar enough with the Parsley internals to make a recommendation on how to avoid the circular references. But it does look like ParsleyForm.fieldsMappedById might be a solid way to get the ParsleyField for a specific form field (which should be storing the parsleyId internally already.

@marcandre
Copy link
Collaborator

Parsley supports <input>s that are not in a form, so parent could also be the global Parsley object.

There's some other complications in that the options need to be restructured if an input is first initialized without a form and later bound via a form (see factory.js and call to _resetOptions).

No offense intended, but I feel you are trying to fix the wrong problem. Using circular references is not an issue that needs resolving. Doing a naive deep clone on third party data is the issue.

@ghost
Copy link

ghost commented Jan 4, 2016

+1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants