0

编辑:

我认为这是因为动作是相同的或什么的。我尝试使用以下方法修改操作:

function mytheme_user_profile_form($form) {
        global $user;
        $uid = $user->uid;
        //print '<pre>'; print_r($form); print '</pre>';
    $category = $form['_category']['#value'];

    switch($category) {
            case 'account':
            $form['#action'] = '/user/'.$uid.'/edit?destination=user/'.$uid;
                        break;
        case 'education':
                        $form['#action'] = '/user/'.$uid.'/edit/education?destination=user/'.$uid;
                        break;
        case 'experience':
                        $form['#action'] = '/user/'.$uid.'/edit/experience?destination=user/'.$uid;
                        break;
            case 'publications':
                        $form['#action'] = '/user/'.$uid.'/edit/publications?destination=user/'.$uid;
                        break;
        case 'conflicts':
                        $form['#action'] = '/user/'.$uid.'/edit/conflicts?destination=user/'.$uid;
                        break;
    }

        //print '<pre>'; print_r($form); print '</pre>';
        //print $form['#action'];
        $output .= drupal_render($form);
        return $output;
}

但是,实际呈现表单时的表单操作是不变的。他们都是 /user/%uid

我可以修改表单操作吗?

我在一页上包含了用户配置文件表单的几个不同“类别”,并且代码将正确输出我指定的表单。每个表单都在一个单独的可折叠 div 中。

我的问题是双重的。

(1) 未预先填充字段的现有值,并且

(2) 点击一个部分的“保存”将导致警告:电子邮件字段是必填项,无论您实际保存的是哪种形式

我很确定对于问题 #2,这是因为按钮的名称在所有情况下都是相同的,表单 id 也是如此。

print '<h3>&ndash; Account Settings</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'account'));
print '</div>';

print '<h3>&ndash; My Info</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'Personal'));
print '</div>';

print '<h3>&ndash; Experience</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'experience'));
print '</div>';

print '<h3>&ndash; Education</h3>';
print '<div class="expand">';
print(drupal_get_form('user_profile_form', $user, 'education'));
print '</div>';
4

1 回答 1

1

问题1:?你能发布html源代码吗?对于问题 #2:好的,我将在此处逐步介绍代码:用户配置文件表单 ( user_profile_form_validate()) 调用的验证处理程序

user_module_invoke('validate', $form_state['values'], $form_state['values']['_account'], $form_state['values']['_category']);

看起来像

<?php
/**
* Invokes hook_user() in every module.
*
* We cannot use module_invoke() for this, because the arguments need to
* be passed by reference.
*/
function user_module_invoke($type, &$array, &$user, $category = NULL) {
  foreach (module_list() as $module) {
    $function = $module .'_user'; 
    if (function_exists($function)) {
      $function($type, $array, $user, $category);
    }
  }
}
?>

因此,此表单的验证处理程序正在遍历每个模块,寻找用户挂钩函数并使用$type = 'validate'. (请注意,'category' 参数在这里是可选的 - contrib 模块不需要使用它)

让我们以 user.module 的 user hook 为例,看看会发生什么:

function user_user($type, &$edit, &$account, $category = NULL) {
  if ($type == 'view') {
    $account->content['user_picture'] = array(
      '#value' => theme('user_picture', $account),
      '#weight' => -10,
    );
    if (!isset($account->content['summary'])) {
      $account->content['summary'] = array();
    }
    $account->content['summary'] += array(
      '#type' => 'user_profile_category',
      '#attributes' => array('class' => 'user-member'),
      '#weight' => 5,
      '#title' => t('History'),
    );
    $account->content['summary']['member_for'] = array(
      '#type' => 'user_profile_item',
      '#title' => t('Member for'),
      '#value' => format_interval(time() - $account->created),
    );
  }
  if ($type == 'form' && $category == 'account') {
    $form_state = array();
    return user_edit_form($form_state, (isset($account->uid) ? $account->uid : FALSE), $edit);
  }
//<-- LOOK HERE -->
  if ($type == 'validate' && $category == 'account') {
    return _user_edit_validate((isset($account->uid) ? $account->uid : FALSE), $edit);
  }

  if ($type == 'submit' && $category == 'account') {
    return _user_edit_submit((isset($account->uid) ? $account->uid : FALSE), $edit);
  }

  if ($type == 'categories') {
    return array(array('name' => 'account', 'title' => t('Account settings'), 'weight' => 1));
  }
}

因此,它只应该验证类别 == 'account'

在函数_use_edit_validate中,我们发现:

  // Validate the e-mail address:
  if ($error = user_validate_mail($edit['mail'])) {
    form_set_error('mail', $error);
  }

有你的错误信息。

由于该表单只应该在类别 == 'account' 时进行验证,而您的问题(#2)似乎是不管类别如何,它总是验证,也许您的表单没有被呈现为唯一的表单实例?Drupal 可能每次都渲染一个完整的表单,并且只是将隐藏的表单值设置为任何类别(如 user_pages.inc 中此表单的定义函数$form['_category'] = array('#type' => 'value', '#value' => $category);

查看实际的 html 源输出会很有帮助。

==编辑 10-15-09 以响应更新的问题===

好的,看起来您的方法($form['#action']在主题层中手动编辑)可能无法实现(请参阅帖子以供参考)。如果您想更改表单操作,您需要编写一个自定义模块来实现hook_form_alter()(它在主题模板文件中不起作用)。此功能允许您修改表单的呈现方式,在您的情况下是用户修改表单。这里有更多关于表单修改的细节。

我不是 100% 确定这是你想要做的;(因为看起来您已经必须创建一个模块)也许您想改为挂钩hook_user();此功能“...允许模块在对用户帐户执行操作时做出反应。”。您可以对此功能中的类别做出反应,并阻止/允许您喜欢的任何用户更改。

但是,如果问题只是电子邮件地址验证,并且如果您正在与现有用户打交道,为什么不在保存之前确保设置了电子邮件地址?

于 2009-10-15T01:41:31.000 回答