I made my custom module, and wanted to do some staff in the validation and submission handlers. My Fields are in two field sets:

  • Member profile, with username and password
  • Company profile, with company name, URL, location, and logo

I am also using the #states attribute on some fields, but everything works except the validation and submission handlers.

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case 'user_profile_form':
      // Only pre-render works.  
      $form['#pre_render'][] = 'mymodule_user_profile_form_pre_render';
      // I try to empty fields with states attribute, but without success. Disabled work OK.
      $form['field_company_url']['und']['0']['value']['#states'] = array(
       'empty' => array(':input[name="field_company_confidential[und]"]' => array('checked' => TRUE)),
       'disabled' => array(':input[name="field_company_confidential[und]"]' => array('checked' => TRUE)),);

      // These don't work as expected.
      $form['#validate'][] = 'mymodule_user_profile_form_validate';
      $form['#submit'][] = 'mymodule_user_profile_form_submit';

      // I also tried this one.
      // $form['actions']['submit']['#submit'][] = 'mymodule_user_profile_form_submit'; 

function mymodule_user_profile_form_submit($form, &$form_state) { 
  // I want to download the image from another website and set as logo.

function mymodule_user_profile_form_validate($form, &$form_state) { 
  //I need to check if checkbox is checked to empty all other fields.
  if ($form_state['values']['field_company_confidential'] == TRUE) {
    $form_state['values']['field_company_url']['und']['0'] = '';

  return $form;

When I try to access $form_state['values'], I don't get any value.


What I am doing wrong?

  • 2
    What do you get when you do a dpm($form['#submit']) after you have added yours? What happens if you add at the beginning of the array, with array_unshift()? For submit, it's possible that someone's (another submit, or a hook) is doing a drupal_goto() or something like that. Try using the form_id specific form_alter and dpm() #submit in form.inc, right before they are called.
    – Berdir
    Jun 16, 2012 at 22:25
  • I want in validate function to empty all company fields if chekbox is checked. In submit function i need to download logo from other website and use that picture as logo.
    – miske
    Jun 17, 2012 at 8:39
  • As side note, form validation, and submission handlers don't return any value; the return $form; line needs to be removed.
    – apaderno
    Jun 17, 2012 at 15:54
  • Yes I removed line {return $form;}. I always try to var_dump('some text'); In all function, and when I put that in validate or submit or just try dpm($form); notheng happens.
    – miske
    Jun 17, 2012 at 15:57

1 Answer 1


The main problem was with devel module. When I disable it and try again all job everything works perfect. So maybe was some bugs with devel module, but I am not sure.

  • I think that has to be related to hooks registry. Everytime you add or remove a hook implementation in your custom module, you have to visit the module list page (no need to deactivate or activate modules) to let Drupal refresh its hooks registry.
    – ermannob
    Jan 21, 2013 at 19:34

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.