简体   繁体   English

需要Drupal-6 CCK字段,具体取决于其他字段的值

[英]Drupal-6 CCK field required depending on another fields value

I have a form that has a bunch of CCK fields, one of which is a master "request type" field. 我有一个包含一堆CCK字段的表单,其中一个是主“请求类型”字段。

The other fields on the form are required depending on the state of the master field. 表单上的其他字段是必需的,具体取决于主字段的状态。

Example form: 范例表格:


{master field} {主字段}

{field one} (required always, set by cck form) {field one}(始终必填,由cck格式设置)

{fieldgroup my group for vertical tabs} { {fieldgroup我的群组以使用垂直制表符} {

-{field two} (required IF master field == '1') -{字段2}(必需的IF主字段=='1')

-{field three} (required IF master field == '1' OR '2') -{字段三}(如果主字段=='1'或'2',则为必填项)

} }


I have been trying to accomplish this using the form validation, at this stage Im trying to make the field required on the fly without even checking the masterfield but I cant make it render as a required field on the form... 我一直在尝试使用表单验证来完成此操作,在此阶段,我试图在不检查masterfield的情况下即时创建必填字段,但我无法使其成为表单上的必填字段...

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';
 }

When I submit the form all I get is Field One field is required. 当我提交表格时,我得到的Field One field is required.

When I do a print_r dump on the $form in the validation function, it shows that I am successfully changing the values but its not rendering as required. 当我在验证功能中的$ form上执行print_r转储时,它表明我已成功更改了值,但未按要求呈现。

How do I make a field required depending on another fields value? 如何根据另一个字段的值使字段为必填字段?

I figured out the key was not to use $form['#validate'][] but $form['#after_build'][] , like this 我发现关键不是使用$form['#validate'][] $form['#after_build'][]而是$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;
  }

Works a treat 工作请客

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM