0

I want to alter a form in my drupal 7 project. I want to activate or deactivate requirement status of a field programmatically based on another field value.

if field value is male , military status field must be required but if it is female, military status is not required and also it must be hide.

Please guide me in this issue.

1

What you could do is make the military field not required. Then add your own validate function to the form with:

$form['#validate'][] = 'my_function_name';

Then in your function you can do a check e.g.

function my_function_name*$form, &$form_state){
  if($form_state['values']['my_gender_field'] == 'male' && empty($form_state['values']['my_military_field'])){
     form_set_error('my_military_field', 'Military field is required');
  }
}

Or you could try using #states from the form API which has a 'required' option: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x#states

| improve this answer | |
  • it is good but I want to call some codes via ajax when a dropdown menu value is changed. imagine that I want to delete a form element in ajax called function. I have added ['#ajax'] and callbacks to form inside module_form_alter , but I don't know how can I remove a form element from or add new from element to form object inside ajax_called_function. can you help me? – Mehrdad201 Jul 4 '18 at 16:12
1

In order to test a solution to your needs I've used a form alter for the article. I've added few comments in the code, I hope they will be enough.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function my_module_form_article_node_form_alter(&$form, &$form_state) {
  $form['gender'] = [
    '#type' => 'select',
    '#title' => t('Gender'),
    '#options' => [
      '_none' => t('Please select gender'),
      'm' => t('Male'),
      'f' => t('Female'),
    ],
    '#default_value' => '_none',
    // Ajax info:
    '#ajax' => [
      // This method returns an array of ajax commands or a form element already prepared.
      'callback' => 'my_module_ajax_responder',
      // This will tell wich dom id will be replaced with the response.data
      'wrapper' => 'military_status_dom_id',
      // pretty obiouvs
      'method' => 'replace',
      'effect' => 'fade',
    ],
  ];

  // Required property has been set to FALSE as default because no gender has been selected yet
  $required = FALSE;
  // this if will be ignored on page load, it means no submit has been done yet.
  if (!empty($form_state['triggering_element']) && $form_state['triggering_element']['#name'] === 'gender') {
    // but if user selects a gender, there will be an ajax submit and this will change 
    // the required property of the military_status texfield element that is going to be
    // rebuilt
    $required = $form_state['triggering_element']['#value'] === 'm';
  }
  // now the actual military_status texfield element.
  $form['military_status'] = [
    '#prefix' => '<div id="military_status_dom_id">',
    '#suffix' => '</div>',
    '#type' => 'textfield',
    '#title' => t('Military status'),
    '#required' => $required,
  ];
}

/**
 * Ajax responder.
 */
function my_module_ajax_responder($form, &$form_state) {
  // after submit and form rebuild, ajax knows that it has to call this method
  // to recover a response data.
  // This data will be used to be a replacement (in this case) for the wrapper 
  // specified in the $form['gender'] element.
  return $form['military_status'];
}

Here some more explatanation:

  • Form will be built and then altered, therefore your alter function will be called
  • there is an if to check if this is a phase of build during the page load or during the ajax submit
  • in this case we tell to drupal that as soon as a user select a gender it has to submit their choice and let us check it, checnge the required property of the military_status field and finally call the "callback" function to retrieve the modified element that will be replaced
| 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.