Don't really know if this is a tableselect issue or a noderefence issue but here goes:

I'm trying to implement the tableselect element in a hook_form_alter on a cck nodereference checkboxes form. If I just alter the checkboxes type to a tableselect no options are presented in the table. And if I load in the options myself it doesn't seem to pass the cck validation. Does anyone have an idea of how to aproach this the best way? You will most definatly save someones ass xD.

Here is what I've got so far:

$options = relation_nodes_get_options(); //copy of the "function _nodereference_potential_references_views(....)" to get the options from a certain view.

$header = array(
  'title' => 'Name',
  'rendered' => 'Price',
);

$form['field_sales_item']['#type'] = 'tableselect';
$form['field_sales_item']['#header'] = $header;
$form['field_sales_item']['#options'] = $options;
$form['field_sales_item']['#parents'] = array(
  0 =>'field_sales_item',
  1 =>'nid',
  2 =>'nid',
  );
$form['field_sales_item']['#array_parents'] = array(
  0 =>'field_sales_item',
  1 =>'nid',
  2 =>'nid',
  );

When I submit this form I get an "invalid input" message from nodereference validation. What am I overlooking here?

Comments

Dave Reid’s picture

Status: Active » Postponed (maintainer needs more info)

What is the result/data structure of relation_nodes_get_options()?

FrUitCaKe.be’s picture

The result is an array of options and each option is structured like this:

[166] => Array ( [title] => mytitle [rendered] => extra fields ) where 166 is the 'nid'

Whenever I create the form next to the original nodereference form and assign the same parents, the nodereference form accepts and stores the selections from the tableselect form. So maybe by altering the original form I'm doing something to the structure in wich normal checkboxes are processed? I'm really getting confused :(

FrUitCaKe.be’s picture

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

Any idea's?

letapjar’s picture

The only place where "invalid input" occurs in nodereference is in

function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {...

here is the relevant code:

   case 'validate':
      // Extract nids to check.
      $ids = array();
      foreach ($items as $delta => $item) {
        if (is_array($item) && !empty($item['nid'])) {
          if (is_numeric($item['nid'])) {
            $ids[] = $item['nid'];
          }
          else {
            $error_element = isset($item['_error_element']) ? $item['_error_element'] : '';
            if (is_array($item) && isset($item['_error_element'])) unset($item['_error_element']);
            form_set_error($error_element, t("%name: invalid input.", array('%name' => t($field['widget']['label']))));
          }
        }
      }

the invalid input message occurs on line 216 of nodereference.module.

Seems like it is expecting and array with an array key of 'nid' and the actual nid in the value. you have your options array with numeric key corresponding to the nids.

perhaps you could try adding the nid key manually - i.e. after the
function relation_nodes_get_options() gives you and array try

foreach($options as $nid=>$option){
$option['nid']=$nid;
}

this *might* make the validation routine happy.

NancyDru’s picture

Issue summary: View changes
Status: Needs review » Closed (outdated)

6.x no longer supported