修改用户当前密码流程(自定义form表单练习)

function mytest_menu() {
  $items = array();
  $items['user/%user/password'] = array(
    'title' => '修改密码',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('change_user_pass',1),
    'access callback' => 'user_edit_access',
    'access arguments' => array(1),
    'type' => MENU_LOCAL_TASK,
  );
  return $items;
} 


function change_user_pass($form, &$form_state, $account) { 
$form['current_pass'] = array(
    '#type' => 'password',
    '#title' => t('当前密码'),   
    // '#prefix' => '',
    '#maxlength' => 64,
    '#size' => 30,
    '#required' => TRUE,
  );

  $form['pass'] = array(
    '#type' => 'password_confirm',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => '提交',
    '#validate' => array('change_user_pass_validate'),
    '#submit' => array('change_user_pass_submit'),
  );
  $form['#validate'][] = 'user_validate_current_pass';
  return $form;
}

function change_user_pass_validate($form, &$form_state) {
  if (!empty($form_state['values']['current_pass'])) {
    # code...
    $_form_current_pass = $form_state['values']['current_pass'];
    $_hash_form_current_pass = module_invoke('user', 'hash_password', $_form_current_pass);

    if ($_hash_form_current_pass != $user->pass) {
      form_set_error('current_pass', t("Your current password is missing or incorrect; it's required to change the Password."));
    }
  }
}


function  change_user_pass_submit($form, &$form_state) {
  global $user;  
  $new_user = null;  
  $new_user = array(  
     'name' => $user->name,  
     'uid' => $user->uid,  
     'pass' => rtrim($form_state['values']['pass']),  
     'status '=>$user->status ,  
  );          
  $new_user['roles']=$user->roles;  
  user_save($user,$new_user);  
  // $form_state['redirect'] = '';  
  drupal_set_message(t('Password success!'));
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值