backdrop / backdrop-issues

Issue tracker for Backdrop core.
144 stars 39 forks source link

Hidden required term reference or list fields with multiple cardinality (#access = FALSE) produce "required" validation error #6664

Open argiepiano opened 1 month ago

argiepiano commented 1 month ago

Description of the bug

If you hide a required term reference or list field with multiple cardinality in a node (e.g. by using hook_form_alter() EDIT: this issue only appears if you modify the $form array in a form wrapper function, ie, a function that manually calls backdrop_get_form('node_form', $node)), the submission still shows the "FIELD is required" validation error. This seems to happen only with select and checkboxes/radios widgets (not autocomplete).

Steps To Reproduce

  1. Add a term reference to a node with multiple cardinality. Make it required.
  2. Implements hook_form_alter() and set #access to FALSE (you can do so at all the levels of the array for the element) EDIT: this issue seems to happen only if you modify the $form array in a wrapper function, ie, a function that gets the form manually calling backdrop_get_form() and then modifies it and returns it
  3. Try to submit the form and see the validation error

Actual behavior

"Required" validation is shown.

Expected behavior

One would expect that, for fields with #access == FALSE, this validation error would not be triggered.

Additional information

Add any other information that could help, such as:

Originally reported in a Zulip thread.

https://backdrop.zulipchat.com/#narrow/stream/218635-Backdrop/topic/Create.20a.20form.20to.20edit.20a.20single.20managed_file.20field.20of.20a.20node/near/453363539

danksearle commented 1 month ago

Well I have found a way to get the #value set, even though there must still be a bug that causes it to not be set in the first place. I have added a test module that sets up two paths: one approach that, without this patch, fails (broken-form/%node) and another that probably works (working-form/%node) hide_fields.zip

To use it, create a content type with title and two other fields, one text field called field_enter_a_word and another List (text) field that allows multiple values so it is shown as checkboxes. Mark both fields as required.

Create a valid node of that type and save.

Now, visit these two URLs to test (replace with the node ID you just made):

working-form/%node broken-form/%node

The forms should show you the text field only, and not show the checkboxes. The working form will allow you to save. The broken one does not - until you apply this patch.

argiepiano commented 1 month ago

I've edited the description of the issue. Further testing shows that modifying the $form array with hook_form_alter() actually doesn't produce the problem. It only happens when you get the form in a form wrapper function by manually getting it with backdrop_get_form() and then modifying that array there.

It's unclear why this is happening. But, @danksearle, this may help your use case - try modifying the array using MODULE_form_node_form_alter(&$form &$form_state); instead of doing it in the wrapper function.