vivin / regula

Regula: An annotation-based form-validation framework in Javascript
126 stars 24 forks source link

regula.validate() breaks when bound elements have been removed #10

Closed troyji closed 13 years ago

troyji commented 13 years ago

The following breaks:

Add a new regula element to a page using javascript.

Run

regula.bind();

Remove the element using javascript.

Call

regula.validate();

BOOM!

Note: The element that I add and remove does not have an ID. Regula adds one to the element for me. It probably still blows up if one is added, though.

In my scenario, I was using a template library. What was happening is that the element was being destroyed and recreated (a completely different element). I would call bind() before validate() every time to compensate. Unfortunately, regula could not adapt because it was trying to validate non-existing elements. I was able to hack in a fix by simply resetting the 'boundConstraints' and 'validatedConstraints' variables at the top of the bind routine (so that it would not hold on to element information from the first bind). This is not necessarily an ideal solution: I will leave it to vivin, who is the most intimate with the implementation.

vivin commented 13 years ago

Good catch! This should be pretty simple to fix. First is to do what you suggested (so that bind will reset the information, which it probably should be doing, anyway). Next, I can check to see if document.getElementById returns null. Then it means that the element doesn't exist anymore and so it can be removed from boundConstraints.