I have stored a list of form ID's as variables. I want to iterate through each of them (which I can do via the foreach loop) and create functions and form elements dynamically.

Here are two of the functions I'm trying to create dynamically. I have a total of 5 that I will be iterating through.

function modulename_form_formid_alter(&$form, $form_state) {
  if (variable_get('modulename_formid', 1)) {
    $form['#validate'][] = 'modulename_formid_validate';
  }
}


function modulename_formid_validate($form, $form_state) {
  if (!modulename_validate_email($form_state['values']['email'])) {
    form_set_error('email', t('The email address you used is not valid. Please choose another email address.'));
  }
}

I have tried using the eval() function, but I get errors due to the "$form, $form_state" arguments in the function as well as the "[#validate] & equal sign" in the form element.

Comments

Jaypan’s picture

Can you explain in a little more detail about what you are trying to do?

pya26’s picture

I have created a custom module that will validate submitted email addresses via an email verification api. The module can hook into any form based on the form id so that the email addresses can be verified.

The administration/configuration screen for my module allows me to enter a list of the form id's separated via a comma, and presents a list of checkboxes for each form id below. If the checkbox for a form id is selected it means that form's email address should be verified.

Here is where I'm stuck. I would like to loop through all of the submitted form id's from my admin/config screen and if they have a value of TRUE (from being checked) then I want to dynamically build the hook_formid_alter() functions and hook_form_validate callback functions so those form will validate submitted email addresses.

This is the code I have been playing around with, but I get several errors as it tries to loop through.

$form_ids_arrary = explode(',',variable_get('modulename_include_formids'));
foreach ($form_ids_arrary as $key => $value) {
  if(variable_get($value, 1)){
    eval("function modulename_form_{$value}_alter(&$form, $form_state) { $form['#validate'][] = 'modulename_test_2_validate' }");
  }
}
pya26’s picture

I have created a custom module that will validate submitted email addresses via an email verification api. The module can hook into any form based on the form id so that the email addresses can be verified.

The administration/configuration screen for my module allows me to enter a list of the form id's separated via a comma, and presents a list of checkboxes for each form id below. If the checkbox for a form id is selected it means that form's email address should be verified.

Here is where I'm stuck. I would like to loop through all of the submitted form id's from my admin/config screen and if they have a value of TRUE (from being checked) then I want to dynamically build the hook_formid_alter() functions and hook_form_validate callback functions so those form will validate submitted email addresses.

This is the code I have been playing around with, but I get several errors as it tries to loop through.

$form_ids_arrary = explode(',',variable_get('modulename_include_formids'));
foreach ($form_ids_arrary as $key => $value) {
  if(variable_get($value, 1)){
    eval("function modulename_form_{$value}_alter(&$form, $form_state) { $form['#validate'][] = 'modulename_test_2_validate' }");
  }
}
sunilajmani’s picture

I am not sure I understood correctly what is your issue but I think you can modify your code like below and it should work:

function modulename_form_alter(&$form, &$form_state, $form_id) {
  $form_ids_arrary = explode(',', variable_get('modulename_include_formids'));
  if (in_array($form_id, $form_ids_array)) {
    if(variable_get($value, 1)){
      $form['#validate'][] = 'modulename_test_2_validate';
    }
}

The above code will apply validate function for all formid added in $form_ids_arrary array variable.

pya26’s picture

The actual functions need to be created dynamically as well as the body of the functions. I have 4 forms, so when I loop through the 4 form id's I need to dynamically create the functions so that I don't need to manually hard code them everytime I add a new form.