首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于其他字段的字段激活或DeActivate需求状态

基于其他字段的字段激活或DeActivate需求状态
EN

Drupal用户
提问于 2018-07-04 12:14:49
回答 2查看 34关注 0票数 0

我想在我的drupal 7项目中修改一个表单。我希望基于另一个字段值以编程方式activate或停用字段的 requirement状态

如果字段值为男性,则军事状态字段必须是必需的,但如果是女性,则不需要军事身份,也必须隐藏。

请在这个问题上指导我。

EN

回答 2

Drupal用户

发布于 2018-07-04 12:56:18

你能做的就是不需要军事领域。然后将您自己的验证函数添加到表单中:

代码语言:javascript
复制
$form['#validate'][] = 'my_function_name';

然后在你的函数中你可以做一个检查。

代码语言:javascript
复制
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');
  }
}

或者您可以尝试使用表单API中的#state,它具有“必需”选项:https://api.drupal.org/api/drupal/developer!topics!forms_api接口_Cence.html/7.x#状态

票数 1
EN

Drupal用户

发布于 2018-07-15 16:56:32

为了测试满足您需求的解决方案,我为本文使用了一个表单alter。我在代码中添加了很少的注释,希望它们足够了。

代码语言:javascript
复制
/**
 * 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' => '',
    '#suffix' => '',
    '#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'];
}

这里有一些更详细的解释:

  • 形态将被建造,然后被改变,因此你的更改函数将被调用。
  • 在页面加载或ajax提交期间,有一个if来检查这是否是构建的一个阶段。
  • 在这种情况下,我们告诉drupal,一旦用户选择了一个性别,它就必须提交他们的选择,让我们检查它,检查military_status字段所需的属性,最后调用“回调”函数来检索将被替换的修改元素。
票数 1
EN
页面原文内容由Drupal提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://drupal.stackexchange.com/questions/264230

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档