Seems to work better if you make the field not required.... If it's required, it seems the validate mechanism gets in the way somehow.... sees that the field isn't filled (even though it will be, using a newly submitted node) and explodes...

The "fix" is just not to require the field.... a better fix would be nice :-)

Comments

igray’s picture

With this patch, I have been able to mark a nodereference field as required when using addnode. I did not test with a nodereference field that can have more than one type.

--- addnode.module.orig 2007-12-20 10:52:11.000000000 -0600
+++ addnode.module      2008-01-08 11:49:07.000000000 -0600
@@ -175,6 +175,7 @@
             'class' => "addnode_select",
             'id' => "$fieldname",
           ),
+          '#after_build' => array('addnode_fix_nodereference'),
         );

         //loops through the types this field can be.
@@ -277,6 +277,45 @@
 }

 /**
+ * This is called as an #after_build function for each CCK nodereference created by addnode.
+ * It Disables the nodereference's requiredness if it's unused
+ */
+function addnode_fix_nodereference($form_element, &$form_values)
+{
+  //the name of the field the addnode widget is for
+  $current_fieldname = $form_element['#attributes']['id'];
+  //for each field in the form, look for addnode_[blah]
+  foreach($form_values as $key => $value)
+  {
+    //if the field key begins 'addnode_' then it's one of the hidden fields, we use these to identify the names of potential subforms
+    if (strpos($key, 'addnode_') === 0)
+    {
+      //get the fieldname (the bit after the 'addnode_' in the key).
+      $fieldname = substr($key, 8);
+      if ($fieldname == $current_fieldname)
+      {
+        //find out which subform's the source (it will be the value of this key).
+        $source = $value;
+    //if it's not '' then we're creating a new node. (if it is '' then we must be using the select box).
+    //in other words, we disable noderefence validation if $source != ''.
+        $disable = FALSE;
+
+        if (strlen($source)>0) //if we're not using the select box...
+        {
+          $disable = TRUE;
+        }
+
+        if ($disable) //disable this nodereference as it's not being used.
+        {
+          $form_element['#required'] = false;
+        }
+      }
+    }
+  }
+  return $form_element;
+}
+
+/**
  * Called as a main form submit handler. It submits the subforms and gets the nids then puts them into
  * the main node.
  */
Anonymous’s picture

Status: Fixed » Closed (fixed)

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

mxgfx’s picture

This issue should still be open, as the above patch didn't fix the problem. It's a good start though. I'll attempt to make some adjustments if I can.

mxgfx’s picture

Status: Closed (fixed) » Active

My apologies for not opening the issue with the last comment.