0

I want to list node titles in drop-down list programmatically. I have done below code in form_alter hook.

function product_custom_form_alter(&$form, FormStateInterface $form_state, $form_id) {
 if($form_id == 'node_inventory_form') {
  $nids = \Drupal::entityQuery('node')->condition('type','product')->execute();
  $nodes =  \Drupal\node\Entity\Node::loadMultiple($nids);
  $product_title = array();
  $product_title['_none'] = " - None -";
  foreach ($nodes as $node_content) {
     $title = $node_content->getTitle();
     $product_title[$title] = $title;
  }
  $form['product_name']['widget']['#options'] = $product_title;
 }
}

It showing the title list proper but i'm getting this error "Error message The value you selected is not a valid choice." while saving the node.

  • 1
    The submitted option(s) are validated against the known valid options for the field - if the values you're injecting differ form those valid options, that's why you see the message – Clive Sep 18 at 12:36
  • Options value attaching dynamically and it's valid. How to differ if it's valid option or not? – user66981 Sep 19 at 5:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.