Hi

I've met these errors on the node creating/editing form.


Notice: Undefined index: field_... in reference_option_limit_form_alter() (line 309 of reference_option_limit/reference_option_limit.module).
Warning: Invalid argument supplied for foreach() in reference_option_limit_form_alter() (line 309 of reference_option_limit/reference_option_limit.module).
Notice: Undefined index: field_... in reference_option_limit_form_alter() (line 351 of reference_option_limit/reference_option_limit.module).

These errors appear when there another ajax form elements in the form (i.e. file fields). Function reference_option_limit_form_alter() is called every time ajax request is sent, so it tries to find corresponding field values in $form_state['values'], but the problem is that $form_state['values'] does not contain needed values when form submit event was triggered by other elements.

I think it is needed to add condition at the start of the function reference_option_limit_form_alter() to check if it is needed to perform operations on limited fields.

Patch is coming.

Comments

webevt’s picture

Status: Active » Needs review
StatusFileSize
new1.58 KB

Patch is here, but your review is needed.

metastate’s picture

Patch got rid of the error for me. Thanks!

joachim’s picture

Thank you both for the patch and the review.

+++ b/sites/all/modules/contrib/reference_option_limit/reference_option_limit.module
@@ -258,6 +258,30 @@ function reference_option_limit_field_widget_form_alter(&$element, &$form_state,
+        // Triggering element can be a field or a button.
+        if (!empty($form_state['triggering_element']['#field_name']) &&

Took me a moment to figure out what this means.

I *think* it's that the AJAX button could in fact not be anything to do with a field, in which case it doesn't have #field_name set on it.

But surely in that case we could bail much earlier on -- before the foreach and before we initialize $flag, no?

Here's a patch with that change. Could someone try it out please?

BTW, a few minor things for future reference:

diff --git a/sites/all/modules/contrib/reference_option_limit/reference_option_limit.module b/sites/all/modules/contrib/reference_option_limit/reference_option_limit.module

Patches should be rolled from the module's folder, not your site root.

+++ b/sites/all/modules/contrib/reference_option_limit/reference_option_limit.module
@@ -258,6 +258,30 @@ function reference_option_limit_field_widget_form_alter(&$element, &$form_state,
+    ¶

Remember to trim whitespace from your code before rolling a patch!

webevt’s picture

Thanks for your review. The condition that you mentioned is really can be executed earlier.

+      foreach ($form_state['reference_option_limit'] as $field) {
+        // Try to find triggering element field name in the list of matching
+        // fields.
+        if (in_array(
+              $form_state['triggering_element']['#field_name'],
+              $field['fields_match']
+            )) {
+          $match = TRUE;
+        }
+      }

The only thing I can add is a "break" statement in the foreach loop, because if we've found the needed element, there's no any reason to go into the next iteration. The patch is here.

joachim’s picture

Status: Needs review » Fixed

Thanks! Committed and pushed.

git commit -m "Issue #2006798 by WebEvt, joachim: Fixed error message when other form ajax elements are triggered." --author="WebEvt "

bruceci’s picture

StatusFileSize
new12.2 KB

patch do not work

file field upload error disapeared , but limited option not work!not work

bruceci’s picture

StatusFileSize
new8.27 KB

not work

bruceci’s picture

Priority: Normal » Critical
Status: Fixed » Needs work
joachim’s picture

Status: Needs work » Postponed (maintainer needs more info)

Is this the same problem you are reporting here -- #2025975: do not work after update to latest version?

Could you debug to see what has caused the problem?

bruceci’s picture

I don't know, but I know the condition aways ran in , there is no '#field_name' attribute in the 'trigger_element'.

if(empty($form_state['trigger_element']['#field_name'])){

return;
}

bruceci’s picture

I installed drupal 7.22 fresh site with reference_option_limit 7.14, but do not work at all。
reference_option_limit 7.13 with no patch is OK!

taxonomy country , taxonomy city, content type location.

city use entity reference to country , location use entity reference to country and city . city field limit by country.

joachim’s picture

What type of widget are you using to trigger?

bruceci’s picture

I use the normal check box drupal core have。

bruceci’s picture

Hey! I found trigger element with select list widget works fine!

YES! trigger element use checkbox do not work!

may checkbox element state do not have "#field_name"?

joachim’s picture

Status: Postponed (maintainer needs more info) » Active

> may checkbox element state do not have "#field_name"?

Yup. Unfortunately, seems they don't.

It's because when it's a radios or checkboxes element that changes, it's only the *single* checkbox or radio button that comes through as the triggering element.

joachim’s picture

Version: 7.x-1.3 » 7.x-1.x-dev
Status: Active » Needs review
StatusFileSize
new2.31 KB

Could someone try this patch please?

It's working for me with both select and radio widgets, and also with a filefield upload button.

Needs latest from git, as I made some refactoring changes.

socialnicheguru’s picture

patch no longer applies

joachim’s picture

It applies fine for me on branch 7.x-1.x.

bonchak’s picture

Hello,

is it possible to make this module work with Simple hierarchical select module ?

joachim’s picture

I'm not actually sure what the latest patch here fixes. I'm able to use filefields and other ajaxy form elements as normal. I think what's being reported in #7 is in fact a new issue: #2182261: doesn't work if controlling field widget is radios.

joachim’s picture

Issue summary: View changes
Status: Needs review » Fixed

Changing back to fixed.

Status: Fixed » Closed (fixed)

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