Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

Good Day.

I created a custom registration form in Drupal 7 using FAPI.

I managed to add validation and it works great. Now my problem is when saving it to the database. I can't save my custom data/fields(e.g first Name, Last Name, Birthday, etc) to my db. I don't know how to do it. I followed all the tuts here http://drupal.org/node/1063598 but it seems my blob data is 0 byte. Did i miss something wrong? here is my code. Any replies are much appreciated.

function drupal_onrevue1_form_user_register_submit($form, $form_state){


   $edit = array(
          'name' => $form_state['values']['name'], 
          'pass' => $form_state['values']['current_pass'],
          'field_first_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_first_name']))),
          'field_last_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_last_name']))),
          'field_middle_name' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_middle_name']))),
          'field_year_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_year_select']))),
          'field_month_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_month_select']))),
          'field_date_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_date_select']))),
          'field_gender_select' => array(LANGUAGE_NONE => array(array('value' => $form_state['values']['field_gender_select']))),
          'mail' => $form_state['values']['field_mail'],
          'init' => $form_state['values']['field_mail'], 
          'status' => 1, 
          'access' => REQUEST_TIME,
    );
    user_save(drupal_anonymous_user(), $edit);
}

function form_user_register($form_state) {
    $form['#validate'][] = 'drupal_onrevue1_validate_register_form';
    $form['#submit'][] = 'drupal_onrevue1_form_user_register_submit';
    $month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October','November', 'December');
    for($i=1; $i < 32; $i++){
        $date[] = $i;
    }

    for($i=date("Y"); $i > 1900; $i--){
        $year[] = $i;
    }


    $form['field_first_name'] = array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#attributes' => array('class' => array('reg_input _con'), 'placeholder' =>'First Name'),
    );
    $form['field_last_name'] = array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#attributes' => array('class' => array('reg_input _con'), 'placeholder' =>'Last Name'),
    );
    $form['field_middle_name'] = array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#maxlength' => '2',
        '#attributes' => array('class' => array('reg_input1 _con'), 'placeholder' =>'M.I'),
    );

    $form['field_gender_select'] = array(
        '#type' => 'select',
        '#attributes' => array('class' => array('drop_filed')),
        '#required' => TRUE,
        '#empty_option' => '- Gender -',
        '#options' => array(
            1 => t('Male'),
            2 => t('Female'),
        ),
    );

    $form['field_month_select'] = array(
        '#type' => 'select',
        '#attributes' => array('class' => array('drop_filed _con')),
        '#required' => TRUE,
        '#empty_option' => '- Month -',
        '#options' => $month,
    );

    $form['field_date_select'] = array(
        '#type' => 'select',
        '#required' => TRUE,
        '#empty_option' => '- Date -',
        '#attributes' => array('class' => array('drop_filed1 _con'), 'style'=>'width:70px;'),
        '#options' => $date
    );

    $form['field_year_select'] = array(
        '#type' => 'select',
        '#required' => TRUE,
        '#empty_option' => '- Year -',
        '#attributes' => array('class' => array('drop_filed1'), 'style'=>'width:80px;'),
        '#options' => $year,

    );


    $form['field_mail'] = array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#attributes' => array('class' => array('reg_input _3 '), 'placeholder' =>'Email Address'),
    );

    $form['name'] = array(
        '#type' => 'textfield',
        '#required' => TRUE,
        '#attributes' => array('class' => array('reg_input _3'), 'placeholder' =>'Username')
    );

    $form['current_pass'] = array(
        '#type' => 'password',
        '#required' => TRUE,
        '#attributes' => array('class' => array('reg_input _3 '), 'placeholder' =>'Password'),
    );

    $form['field_repassword'] = array(
        '#type' => 'password',
        '#required' => TRUE,
        '#attributes' => array('class' => array('reg_input _3 '), 'placeholder' =>'Re-type Password'),
    );

    $form['actions']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Submit'),
        '#attributes' =>  array('class' => array("btn btn-medium btn-success"), 'style'=>'width:60px'), 
    );  

    return $form;   
  }
share|improve this question
add comment

1 Answer 1

use &$form_state instead of $form_state in function name it should be

 function drupal_onrevue1_form_user_register_submit($form, &$form_state)

U have stated blob data as 0 byte. Are the other fields getting stored or not? for viewing values in submit use drupal_set_message() to check the values at runtime.

share|improve this answer
    
Not working... yes...my data is 0 byte in table user...other fields has been stored like the mail, username, pass, status...but other than that it can't save...my question where should first_name, last_name, middle_name, etc. will be saved? at the data fields? or anywhere?? Thanks –  Junnel Gallemaso Jul 27 '12 at 9:15
    
It will be saved at data fields... check the table whether the names of fields are same as you have given in your code.... if other fields like mail,username are getting stored it means values are there in submit function –  Dharmender Jul 27 '12 at 9:21
    
yeah....I can save the username,password, status... But the first_name, last_name, etc...can't save to the data field (blob is in 0 byte) –  Junnel Gallemaso Jul 27 '12 at 9:25
    
If i am not mistaken....other custom fields (e.g fist_name, last_name, middle_name) will be serialized and save to the data column right? –  Junnel Gallemaso Jul 27 '12 at 9:27
    
fields have to be serialized.... –  Dharmender Jul 27 '12 at 9:42
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.