Because the javascript is added to the page directly, if the form is rebuilt from cache, as happens when there is a validation failure, the javascript is lost. To demonstrate this bug, try to add a node with a blank title. After the validation failure, note that uniqueness no longer works.

For 7.x, the fix is easy: use the new $form['#attached'] FAPI option to attach the jQuery and settings to the form.

For 6.x, the javascript will have to be added in an after_build function, rather than the alter function. The after_build is called after when the form is rebuilt from cache.

I will post up a patch for 7.x, along with a little clean-up, once I get back in sync on other outstanding issues.

CommentFileSizeAuthor
#2 validation_loses_js-1893458-2.patch3.69 KBdanchadwick

Comments

bforchhammer’s picture

Uh, that's bad. For 7.x the #attached thing is definitely the right way forward. We need to make sure it works with the block as well as the inline widget.

For 6.x there's already #1316200: Uniqueness widget doesn't work if form validation errors appear which sounds related and already has a patch. Looks like it just needs testing and verification...

danchadwick’s picture

Status: Active » Needs review
StatusFileSize
new3.69 KB

Took the opportunity to clean things up a bit. Also noticed that the preview was not being set properly because the function didn't have access to $form_state. I'm passing the preview setting into the function now.

Also because we are injecting the js into the form element, $form['uniqueness'] is now guaranteed to be set, so I removed the if (isset()) for it. I also added the uniqueness_type into the array that is added to the existing array, eliminating the need for the isset test on that too. A bit cleaner, I think, but if you disagree feel free to put it back.

Again, thanks for your help. Lot's of progress in the last day!

danchadwick’s picture

PS I tested it in inline, in the block, and both, as well as in preview. All seems fine and I see no reason why it wouldn't be.

bforchhammer’s picture

Status: Needs review » Fixed

Thanks.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.