2009-10-14 105 views
0

编辑:Drupal - 在页面上包含多个user_profile_form

我认为这是因为动作是相同的或某事。我试图修改使用这个动作:

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>'; 
+0

你looekd,从源头上看看这些形式实际上是独一无二的? – mikewaters 2009-10-15 01:30:42

回答

1

问题1:?你能发布html源码吗? 对于问题2: OK,我将逐步这里的代码: 为用户配置文件的形式(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'来调用它们。 (请注意,“类别”参数是可选在这里 - 使用它的contrib模块不需要)

让我们来看看user.module的用户挂机为例,来看看会发生什么:

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

所以,只应该如果类别==“账户”

在功能_use_edit_validate验证,我们发现:

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

有你的错误消息。

由于该窗体只应验证类别=='帐户',和您的问题(#2)似乎是它始终验证,无论类别,也许您的窗体不呈现为唯一窗体实例? Drupal可能每次都会呈现一个完整的表单,并且只要将隐藏表单值设置为任何类别即可(例如,在此表单的定义函数中,user_pages.inc $form['_category'] = array('#type' => 'value', '#value' => $category);

查看实际的html源输出。

==编辑零九年十月十五日响应更新的问题===

OK,它看起来像你的方法(在主题层手动编辑$form['#action'])可能无法(见this后供参考)。如果你想改变表单动作,你需要编写一个自定义模块来实现hook_form_alter()(它不能在主题模板文件中工作)。这个功能允许你修改表单的渲染方式,在你的情况下用户修改表单。表格修改here有更多详细信息。

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

但是,如果它是有问题的,如果你正在处理的现有用户,你为什么不只是确保你之前保存的电子邮件地址被设置不仅仅是电子邮件地址验证?

+0

是的,这就是问题所在......我正在编辑我的原始文章,详细阐述一下... – n00b0101 2009-10-15 03:08:10

相关问题