1

I have a form with 2 buttons. One is a submit button and another is a simple button when clicked, the page has to be redirected to another page.(I mean the user has to be redirected even if he hasn't filled in the entire form)

So after the submit button I put the following code for the other element

<?php 
$form['health_calorie_consume']['view'] = array(
          '#type'=>'button',
      '#value'=>'View stats',
    '#validate'=>array('mymodule_form_view'),
     ); 
?>

inside the mymodule_form_view() function, I put the below code,

<?php
function mymodule_form_view()
  $form_state['redirect'] = 'http://localhost/d7/?q=node/3';
?>

To my surprise, Thhe function gets called but its not redirected..! and shows errors that some of the form fields with #reuired attribute haven't been filled out..! I tried using drupal_goto()function, it redirects to the other page properly but even in the redirected page those, "required" errors show up..!!

I even tried making #required attributes FALSE, inside the mymodule_form_view()and yet it didn't work.

So how to solve this problem..??

0

You can use: #limit_validation_errors to skip validation:

$form['health_calorie_consume']['view'] = array(
  '#type'=>'submit',
  '#value'=>'View stats',
  '#limit_validation_errors' => array(),
  '#submit' => array('mymodule_form_view'),
);

Your mymodule_form_view function needs fixing also like mechler pointed out. Besides passing it $form and $form_state variables you may want to use the url() function for building the redirect url.

function mymodule_form_view($form, &$form_state) {
  $form_state['redirect'] = url('node/3', array('absolute' => TRUE));
}
0

Modifying $form_state requires passing it by reference to the function.

function mymodule_form_view(&$form, &$form_state) {
  $form_state['redirect'] = 'http://localhost/d7/?q=node/3';
}
  • Nope..:( its not redirecting..! As I said, drupal_goto() works but in the redirected node, i.e, node/3 those errors that say that some fields are required are getting displayed. – akshaynhegde Mar 4 '12 at 16:28

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.