【问题标题】:Drupal-6 CCK field required depending on another fields valueDrupal-6 CCK 字段需要取决于另一个字段值
【发布时间】:2012-12-01 22:31:03
【问题描述】:

我有一个包含一堆 CCK 字段的表单,其中一个是主“请求类型”字段。

表单上的其他字段是必需的,具体取决于主字段的状态。

示例形式:


{主字段}

{field one}(始终需要,由 cck 表单设置)

{fieldgroup 我的垂直标签组} {

-{字段二}(必需的 IF 主字段 == '1')

-{字段三}(必需的 IF 主字段 == '1' OR '2')

}


我一直在尝试使用表单验证来完成此操作,在这个阶段,我试图在不检查主字段的情况下即时使该字段成为必填字段,但我无法将其呈现为表单上的必填字段...

function my_module_form_alter(&$form, $form_state, $form_id) {
  switch ($form_id) 
  {
   case 'my_node_node_form':
    $form['#validate'][] = 'my_module_form_validate';
    break;
   default:
    // nothing
    break;
  }
 }

function my_module_form_validate($form, &$form_state) {
  // there are so many versions of the same field, which one do I use?
  $form['#field_info']['field_two']['required'] = '1'; 
  $form['group_my_group']['field_two']['#required'] = '1';
  $form['group_my_group']['field_two'][0]['#required'] = 'TRUE';
 }

当我提交表单时,我得到的只是Field One field is required.

当我在验证函数中对 $form 执行 print_r 转储时,它表明我已成功更改值,但未按要求呈现。

如何根据另一个字段值使一个字段成为必填字段?

【问题讨论】:

    标签: php validation drupal drupal-6 cck


    【解决方案1】:

    我发现关键不是使用$form['#validate'][],而是$form['#after_build'][],就像这样

    function my_module_form_alter(&$form, $form_state, $form_id) {
      switch ($form_id) 
      {
       case 'my_node_node_form':
        $form['#after_build'][] = 'my_module_form_after_build';
        break;
       default:
        // nothing
        break;
      }
     }
    
    function my_module_form_after_build($form, &$form_state) {
      switch ($form['field_master_field']['#value']['value']) {
        case '2':
          $form['group_my_group']['field_three'][0]['value']['#required'] = TRUE;
        case '1':
          $form['group_my_group']['field_two'][0]['value']['#required'] = TRUE;
        case '0':
          $form['group_my_group']['field_one'][0]['value']['#required'] = TRUE;        
         break;
        }
       return $form;
      }
    

    工作愉快

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多