1

how do i automatically submit a form programatically without user click submit button.
what i want to do is that i have an two nodes of content type fact box and basic page. The content type fact box has an entity referenced field referenced to the basic page.

I want to validate the entity reference field in such a way that it validates the title field of the corresponding fact box, if the validation returns false then output an error message indicating the error to the entity reference field.

The logic i used is that after selecting the basic page from the entity reference page redirect to the basic page edit page and then validate the title field of the basic page, but how do i get this, i need to auto submit the form so as to validate the title field.

Here is what i have done so far:

function override_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    // for fact box
    case 'factbox_node_form':
      $form['actions']['submit']['#submit'][] = 
       'override_factbox_custom_submit';
      $form['#validate'][] = 'override_factbox_form_validate';
    break;

    // for basic page
    case 'page_node_form':
      $form['actions']['submit']['#submit'][] = 
       'override_basic_page_custom_submit';
      $form['#validate'][] = 'override_basic_page_form_validate';
    break;
  }
}

/**
 * Implements hook_form_validate() for factbox.
 */
 function override_factbox_form_validate($form, &$form_state) {
   // get the id of the selected basic page.
   $target_id = $form_state['values']['field_main_page']['und']['0']['target_id'];
   // get the edit page of selected basic page
   $node_edit = 'http://localhost/drupal2/node/'.$target_id.'/edit';

   // redirect to the edit page of selected basic page
   drupal_goto($node_edit);
 }

 // now on the basic_page/edit of the node selected.
 // I need an auto submit form here so to implement the validation below.

 /**
  * Implements hook_form_validate() for basic page.
  */
  function override_basic_page_form_validate($form, &$form_state) {
    // check if the value of the title field is less than 10
    if (strlen($form_state['values']['title']) < 10){
      // if true redirect back to previous page
      drupal_goto($previous_url);
    }
  }
 //output drupal error message indicating the entity reference field

any opinion on how to get this done??
thanks in advance.

  • As side note, PHP has just positional parameters, so you don't need to write drupal_goto($path = $node_edit);. – kiamlaluno Mar 3 '18 at 22:07
  • what do you mean by positional parameters – Fresher Mar 8 '18 at 8:14
  • I mean that in PHP the arguments are assigned basing on their position. For drupal_goto() the first argument you pass to the function is the path, the second argument is an array of options, and the third argument is the HTTP status code to use for the redirection. You don't need to write drupal_goto($path = $node_edit); because the path is the first argument for drupal_goto(). – kiamlaluno Mar 8 '18 at 9:07
  • Actually, with drupal_goto($path = $node_edit); you are assigning to the variable $path the value of $node_edit, and calling drupal_goto() with the value of $node_edit. Just use drupal_goto($node_edit); and you are done. – kiamlaluno Mar 8 '18 at 9:15
  • any opinion on how to get that problem solved?? – Fresher Mar 8 '18 at 17:13

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.