简体   繁体   English

在user_register_form drupal 7中为用户名字段提供默认值

[英]Providing Default value to username field in user_register_form drupal 7

/**
 * Implements hook_form_alter()
 */
function voen_registration_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id){
    case 'user_register_form':
      $form['account']['name']['#type'] = 'hidden';
      array_unshift($form['#validate'],'_voen_registration_custom_validate');  
      array_unshift($form['#submit'],'_voen_registration_generate_username');
      break;
  }     
}

function _voen_registration_generate_username(&$form, &$form_state){
  drupal_set_message('Function Running');
}

/**
 * Since the validation functions runs before submit
 * We are assigning value to the username field so that it passes 
 * the default validation function
 */
function _voen_registration_custom_validate(&$form, &$form_state){
  $form_state['values']['name'] = 'Abc Name';
}

Hi, Thy goal i want to achieve from the above code is that i want to hide the username field from the registration form and instead i want to concatenate the first and last name to make username by code before it goes to database. 嗨,我要从上述代码中实现的目标是,我想从注册表单中隐藏用户名字段,而我想将姓和名串联起来,以便在进入数据库之前通过代码创建用户名。

However the problem is that the default validation of the user_register_form occur before my any code. 但是,问题在于在我的任何代码之前都会进行user_register_form的默认验证。 So i am trying to invoke my custom validation method before the default validation of user_register_form so that it sets the default value of username field to ABC Name. 因此,我尝试在对user_register_form进行默认验证之前调用我的自定义验证方法,以便将用户名字段的默认值设置为ABC名称。

But still now when i submit the form it shows me the error of username is required. 但是现在,当我提交表单时,它向我显示了用户名错误是必需的。

How can i fix this. 我怎样才能解决这个问题。 I have to do this via code not any other module. 我必须通过代码而不是其他任何模块来执行此操作。

好的,我将用户名字段的#needs_validation属性设置为false来解决此问题

$form['account']['name']['#needs_validation'] = false;

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

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