Ask twice for email in the Ubercart checkout page - drupal

Is there a Ubercart module to ask the user to insert his email twice in the checkout page?

There is an email confirmation checkbox in ubercart checkout settings. No additional modules needed.

I doubt there is a module for this. You can do this with hook_form_alter in a custom module. Should only be 10-20 lines of code.
Something like
function module_form_FORM_ID_alter(&$form, &$form_state) {
$form['...']['second_mail'] = array(
'#title' => t('Verify E-mail'),
'#type' => 'textfield',
'#weight' => xx,
);
$form['#validate'][] = 'module_validate_function_name';
}
function module_validate_function_name(&$form, &$form_state) {
if ($form_state['values']['mail'] != $form_state['values']['second_mail']) {
form_set_error('second_mail', t('You have mistyped your e-mail, please verify');
}
}
The above is example code, but might actually work, it depends how the ubercart checkout form is created, more specifically, the name of it's mail field.
There are a few blanks but it should be easy enough to fill out.

I got it working by using this:
/* Code to add confirm email for uc checkout */
function custom_code_form_alter(&$form, $form_state, $form_id) {
if($form_id == "uc_cart_checkout_form" && $form['panes']['customer']['primary_email']['#type'] != 'hidden'){
$form['panes']['customer']['primary_email']['#weight'] = '0';
$form['panes']['customer']['new_account']['#weight'] = '2';
$form['panes']['customer']['confirm_email'] = array(
'#title' => t('Verify E-mail address'),
'#type' => 'textfield',
'#size' => '32',
'#required' => true,
'#weight' => '1'
);
$form['#validate'][] = 'custom_code_validate_confirm_email';
}
}
function custom_code_validate_confirm_email(&$form, &$form_state){
if($form_state['values']['panes']['customer']['primary_email'] != $form_state['values']['panes']['customer']['confirm_email']) {
form_set_error('panes[customer][confirm_email', t('Email addresses must match.'));
}
}
/* end code for confirm_email */

Related

Drupal:Direct a user to a specific page after submitting

I have a submitt drupal form in a my module:
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
And this is my submitt function , what command have to add to direct the user to a specific page with a specific path? I tried this but it didnt work
function testform_submit($form, &$form_state) {
$form_state['submit'] = 'https://www.google.de/';
}
So close...
function testform_submit($form, &$form_state) {
$form_state['redirect'] = 'https://www.google.de/';
}

Drupal 7 - Confirm email address

I have added some extra fields to the standard 'create account' page; notably a 'confirm email' field.
How do hook into the validation so that I can add some custom validation rules of my own (e.g. to check the two emails match)?
I have found hook_user_presave, but am unsure on how to code it or where I should put it.
Any and all help appreciated.
I would advise installing the LoginToboggan module, it actually has the option for that exact functionality out of the box and has a bunch of other useful options as well.
If you want to do it yourself though you'd probably be better off implementing hook_form_FORM_ID_alter() and adding a validation function directly to the registration form:
function mymodule_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['#validate'][] = 'mymodule_user_register_form_validate';
}
function mymodule_user_register_form_validate(&$form, &$form_state) {
if ($form_state['values']['first_email'] != $form_state['values']['second_email']) {
form_set_error('second_email', 'The email addresses much match.');
}
}
Make sure you clear Drupal's cache once you've implemented the form alter function so Drupal registers it correctly.
Hope that helps.
Here is the example solution for Drupal 7:
/**
* Implements hook_menu().
* Note: You can define your own menu callback optionally.
*/
function foo_menu() {
$items['foo-signup'] = array(
'title' => 'Create new account',
'page callback' => 'drupal_get_form',
'page arguments' => array('user_register_form'),
'access callback' => 'user_register_access',
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function foo_form_user_register_form_alter(&$form, &$form_state, $form_id) {
$form['account']['mail_confirm'] = array(
'#type' => 'textfield',
'#title' => t('Confirm e-mail address'),
'#maxlength' => EMAIL_MAX_LENGTH,
'#description' => t('Please confirm your e-mail address.'),
'#required' => TRUE,
);
$form['#validate'][] = 'foo_user_register_form_validate';
}
/**
* Implements validation callback.
*/
function foo_user_register_form_validate(&$form, &$form_state) {
if ($form_state['values']['mail'] != $form_state['values']['mail_confirm']) {
form_set_error('mail_confirm', 'The email addresses must match.');
}
}

How to add Language switcher in user/register form in Drupal 6

I am trying to add Language settings in user/register form (like in user edit), but I haven't had any success finding a solution through Google.
Any ideas how I can add a user's language settings in the registration form?
Thanks in advance
I haven't managed to find a direct solution as well.
So it seems that a small custom module will be required.
I copied the implementation of hook_user from the locale module and slightly modified it.
So create your custom module and this hook will be enough.
/**
* Implementation of hook_user().
*/
function yourmodulename_user($op, &$edit, &$account, $category = NULL) {
global $language;
// If we have more then one language and either creating a user on the
// admin interface or edit the user, show the language selector.
if ($op == 'register') {
$languages = language_list('enabled');
$languages = $languages[1];
// If the user is being created, we set the user language to the page language.
$user_preferred_language = $user ? user_preferred_language($user) : $language;
$names = array();
foreach ($languages as $langcode => $item) {
$name = t($item->name);
$names[$langcode] = $name . ($item->native != $name ? ' ('. $item->native .')' : '');
}
$form['locale'] = array(
'#type' => 'fieldset',
'#title' => t('Language settings'),
'#weight' => 1,
);
$form['locale']['language'] = array(
'#type' => (count($names) <= 5 ? 'radios' : 'select'),
'#title' => t('Language'),
'#default_value' => $user_preferred_language->language,
'#options' => $names,
'#description' => t("This account's default language for e-mails."),
);
return $form;
}
}

Drupal hook_form_alter anonymous user can't see the fields

I created a small module for altering forms called "form_mods". The form I'm altering is the "user_profile_form". I added a category for extra fields called "Profile".
I created a select field in the Drupal admin called "profile_state" and I'm altering it in my module to have a key => value list of states and It's working for me when logged in as an admin but an anonymous user that's trying to register sees an empty states select field. Is there a permissions issue here? I tried to add 'access' => user_access('access content') to the field but that didn't work. Here is my code:
function form_mods_form_alter($form, $form_state, $form_id) {
switch ($form_id) {
## user_profile_form ###################################################################################
case 'user_profile_form': // This is our form ID.
//echo '###'.$form['_category']['#value'].'###';
if($form['_category']['#value'] == 'Profile'){
// Modify the states dropdown list
$states = load_states_list();
$form['Profile']['profile_state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#options' => $states,
'#required' => TRUE,
'#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : '',
'#element_validate' => array('profile_state_validate')
);
}
####################################################################################
break;
}
}
function load_states_list() {
$states = array('' => '-- Select a state'); // add a default empty value
$results = db_query("SELECT * FROM {states_list} ORDER BY name ASC");
while ($state = db_fetch_array($results)) {
$states[$state['code']] = $state['name'];
}
return $states;
}
Thanks
First of all, are you sure you're function ever gets run? Having the function named the way you do, I don't think it is. If the module name is "form_mods", your function should be called
function form_mods_form_alter
Or...since you're only modifying the user_profile_form form, you could use the function name
function form_mods_user_profile_form_alter
Now, the reason it isn't working is because you don't have the & in front of the $form in the parameter list. The & basically passes the variable as reference, and so any changes you make to the $form variable will be saved and passed back to the calling function. So, your function should look like
function form_mods_form_alter(&$form, &$form_state, $form_id) {
OR
function form_mods_user_profile_form_alter(&$form, &$form_state) {
Reference: http://api.drupal.org/api/drupal/developer--hooks--core.php/function/hook_form_alter/6
Thank you mikesir87 for the reference link. I figured out my problem.
There are 2 different forms that are using those fields I created. They have different form id's. I have to look for "user_profile_form" and "user_register" form id's.
Here is my new code that works:
function form_mods_form_alter($form, $form_state, $form_id) {
if( (($form_id == 'user_profile_form') && ($form['_category']['#value'] == 'Profile')) || ($form_id == 'user_register') ){
// Modify the states dropdown list
$states = form_mods_load_states_list();
$form['Profile']['profile_state'] = array(
'#type' => 'select',
'#title' => t('State'),
'#options' => $states,
'#required' => TRUE,
'#default_value' => isset($form['Profile']['profile_state']['#default_value']) ? $form['Profile']['profile_state']['#default_value'] : ''
);
}
}
thanks

Drupal hook_form_alter Not redirecting

I am using hook form_alter and setting the $form['#redirect'] = 'dir1/dir2/mypage'. But the form refuses to go there.
Form seems to work otherwise, but keeps sending back to the original form instead of the redirect.
The form I am altering is from the root user module.
mymodule_form_alter( ){
... code...
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25, '#description' => t(''),
'#required' => TRUE
);
unset($form['Birthdate']['profile_birthdate']);
unset($form['Birthdate']);
unset($form['#action']);
$form['#validate'] = array('_mymodule_school_registration_validate');
$form['#submit'] = array( '_mymodule_school_registration_submit');
$form['#redirect']= "dir1/dir2/mypage";
}
Please help trying to meet an overdue dead line!! : (
Thanks in advance.
Your hook_form_alter() implementation is not correct:
Without parameters, you're aren't modifying anything, so none of your changes get registered,
$form['#submit'] and $form['#validate'] are already arrays with content, so you should not be resetting them with array(),
unsetting $form['#action'] causes the form to do nothing when submitted,
setting $form['#redirect'] in hook_form_alter() will get overridden by other handlers, and
your hook_form_alter() implementation would affect (and break) every form.
More info: Forms API Reference
Instead, try the following:
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id === 'form_id_goes_here') {
// Code here
$form['account']['pass'] = array(
'#type' => 'password_confirm',
'#size' => 25,
'#description' => t(''),
'#required' => TRUE
);
unset($form['Birthdate']['profile_birthdate']);
unset($form['Birthdate']);
$form['#validate'][] = '_mymodule_school_registration_validate';
$form['#submit'][] = '_mymodule_school_registration_submit';
}
}
function _mymodule_school_registration_submit($form, &$form_state) {
// Code here
$form_state['redirect'] = 'dir1/dir2/mypage';
}
Try
$form_state['redirect'] = "dir1/dir2/mypage";
If you've only got one handler for your submit you could easily redirect with
function _mymodule_school_registration_submit(..args...) {
...
drupal_goto('somewhere');
}
I think the functionality is the same.
http://api.drupal.org/api/function/drupal_goto/6
I tend to avoid redirects so once you have met your deadline I would refactor your code. You can usually get away with out redirecting.

Resources