1

I am using Organic Groups. That module has a form where you add new members to a group. I would like to run additional validation on that form. I use hook_form_alter and can identify the form. I add a validate callback and verify its been added to the form array. Yet that function never runs. I don't know why.

function organization_control_form_alter($form, &$form_state, $form_id){
  if($form_id == 'og_ui_add_users'){
    dpm("Validation Added");
    //This dpm displays. 

    $form["#validate"][] = 'organization_control_og_size_control_form_validate';
    //I have also tried running my validate function first, like this
      //array_unshift($form["#validate"],'organization_control_og_size_control_form_validate');
    //I have also tried only running my validation, like this
      //$form["#validate"] = array('organization_control_og_size_control_form_validate');

    dpm("form");  
    dpm($form);
    //This dpm shows the validate callback has been added. 
  }
}

function organization_control_og_size_control_form_validate($form, &$form_state){
  dpm("Validation Started");
  //This dpm NEVER shows

  //hard code an error for testing
  $subscriptionInfo['field_total_seats_value'] = 4;

  dpm("Validation Run 2");
  //This dpm NEVER shows

  if($organzational_control_organization_size >=  $subscriptionInfo['field_total_seats_value']){
    dpm("ERROR");
    //This dpm NEVER shows

    form_set_error('edit-name', t('Your Organization has reached is current license size limit.'));
    //This error is not displayed and the form submissions continues and succeeds. 
  }
  else{
    dpm("It thinks its Okay");
    //This dpm NEVER shows
  }
}
1

It's not working because you're not using a reference to $form.

Change your function declaration to:

function organization_control_form_alter(&$form, &$form_state, $form_id){

Note the & in front of $form. This means you're actually working on the variable as passed to the function, not a copy, or at least that's my understanding of it.

A useful thing to do to avoid this kind of error with hooks is to check out the API for the hook you are trying to use and copy the parameters verbatim.

| improve this answer | |

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.