Skip to content

Custom fields are reset to 0 when an object is stored outside its edit form #527

Closed

Description

@caseysoftware

Current state

  • Every save reads $_POST. w2p_Core_BaseObject::hook_postStore() (BaseObject.class.php ~line 765) runs on every store(). It builds w2p_Core_CustomFields for the object and calls bind($_POST) and store(), whatever request is running.
  • Missing fields become 0. w2p_Core_CustomFields::bind() (CustomFields.class.php line 158) sets every field that is not in the input to 0. It does that because an unchecked checkbox (CustomFieldCheckbox, line 19) posts nothing.
  • Result: any store() from a request that doesn't contain the object's edit form silently zeroes that object's custom fields. That includes:
    • AJAX or JSON saves;
    • imports (w2p_Actions_ImportTasks);
    • hook_cron work in queuescanner.php;
    • other modules saving an object as a side effect.
  • Worth auditing: a secondary object stored during another object's form post binds the first object's posted values, e.g. tasks updated by a parent or dependency recalculation inside a task save.

Intended solution

  1. Checkboxes always post. CustomFieldCheckbox renders <input type="hidden" name="<field>" value="0"> before the checkbox. A missing key then genuinely means "not in this form".
  2. A form marker. The custom-fields block of an edit form emits a hidden w2p_cf[<module>]=1.
  3. Explicit input. Add BaseObject::setCustomFieldInput(array $input).
    • w2p_Controllers_Base::process() calls it with the request data before store().
    • hook_postStore() only binds and stores custom fields when input was set.
    • For backward compatibility it also binds when $_POST carries the w2p_cf marker for this object's module.
    • It never reads $_POST otherwise.
  4. Bind only what was sent. CustomFields::bind() updates only the keys present in the input and leaves the others untouched.

Acceptance

  • Storing an object from code (no form post) leaves its custom field values unchanged.
  • Unchecking a checkbox custom field in an edit form still saves 0.
  • Saving a task whose save also stores other tasks doesn't change those tasks' custom fields.
  • Unit tests cover all three cases.

Context

Found while designing the Cards module (caseysoftware/modules-cards), which saves cards over AJAX.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions