In debugging my attempts at implementing the form_builder api, I noticed some weird behavior.

The code in form_builder_cache_save() was causing it to always save a new row in the database because my $form started out completely empty. The call to form_builder_cache_load was returning an empty array, which was evaluating to false in the test seen below.

if (form_builder_cache_load($form_type, $form_id, $sid)) {
    $result = db_query("UPDATE {form_builder_cache} SET data = '%s', updated = %d WHERE type = '%s' AND form_id = '%s' AND sid = '%s'", serialize($form), time(), $form_type, $form_id, $sid);
  }
  else {
    $result = db_query("INSERT INTO {form_builder_cache} (sid, type, form_id, updated, data) VALUES ('%s', '%s', '%s', %d, '%s')", $sid, $form_type, $form_id, time(), serialize($form));
  }

As a result, the cache table was full of duplicate rows, but cache_load always returns the first match which was the original empty array.

As a quick fix I implemented the form_builder_load hook and added a hidden input to the form. This caused the aforementioned load to not return an empty array and things seem to be rolling now.

Not sure if this falls more in the category of a faulty implementation or a potential bug in the api iteself.

Please let me know if there is anything I can do to help.

Would a potential fix be to test if the cache_load returns FALSE explicitly? Such as:

if (form_builder_cache_load($form_type, $form_id, $sid) === FALSE) {
    $result = db_query("UPDATE {form_builder_cache} SET data = '%s', updated = %d WHERE type = '%s' AND form_id = '%s' AND sid = '%s'", serialize($form), time(), $form_type, $form_id, $sid);
  }
  else {
    $result = db_query("INSERT INTO {form_builder_cache} (sid, type, form_id, updated, data) VALUES ('%s', '%s', '%s', %d, '%s')", $sid, $form_type, $form_id, time(), serialize($form));
  }
CommentFileSizeAuthor
#1 form_builder_update.patch3.25 KBquicksketch

Comments

quicksketch’s picture

Status: Active » Fixed
StatusFileSize
new3.25 KB

You're definitely correct. I wish I would've found this issue before spending time coming to the exact same solution. I've applied the attached patch which corrects this behavior, using the exact solution you suggested: Use strict comparison operators upon load.

te-brian’s picture

Glad I was able to help. Seemed almost too easy :) Didn't know if I was misreading the situation. I'm definitely excited to continue using form builder, especially with D7.

Status: Fixed » Closed (fixed)

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