2

I am creating a user programatically.

I have following files

$fields = array(

    'name' => $name,
    'mail' => $mail,
    'pass' => $password,
    'status' => 1,
    'init' => $mail,
    'field_first_name' =>array('und'=>array(0=>array('value'=>$first_name))),
    'field_last_name' => array('und'=>array(0=>array('value'=>$last_name))),
    'field_company_name'=>array('und'=>array(0=>array('value'=>$company_name))),
    'field_company_number'=>array('und'=>array(0=>array('value'=>$company_number))),
    'field_mobile_number'=>array('und'=>array(0=>array('value'=>$mobile_number))),
    'field_secondary_number'=>array('und'=>array(0=>array('value'=>$secondary_number))),
    'field_mobile_or_landline'=>array('und'=>array(0=>array('value'=>$landlinemobile))),
    'field_promo_emails_notifications'=>array('und'=>array(0=>array('value'=>$email_notification))),

);

Now i am using user_save(NULL,$fields);

Which is working fine. Now field_first_name, and some other fields are required which i have set under account mangefields. How to check validations of these fields. Does anybody tell me

Thanks

1

Validation is pretty much depends on what kind of fields are they! Still I can show validation for the common fields as on user registration process.

You must be hooking into the user registration form somewhere, unless form is custom form.Assuming you are implementing hook_form_alter() to hook into user registration form.

> Step 1: Add validation handler to the user registration form.

    // Implementing hook_form_alter() to add validation handler to user registration form.
    function mymodulename_form_alter($form,$form_state,$form_id) {

    // Not sure you have to check form id of the user registration form.
      if($form_id == "user_registration") {
        $form['#validate'] = array(
          '#validate' => array('_mymodulename_registration_validate'),
        );
      }
    }

    // Write validation callback.
    function _mymodulename_registration_validate(&$form,&$form_state) {
       // Validate last name.
      if (trim($form_state['values']['last_name']) == '') {
        form_set_error('last_name', 'Last Name field is required field');
      }

      // Validate company name.
      if (trim($form_state['values']['company_name']) == '') {
        form_set_error('company_name', 'Company Name field is required field');
      }

      // Validate the e-mail address.
      if (!valid_email_address(trim($form_state['values']['email']))) {
        form_set_error('email', 'Please enter valid email address');
      }
      if (_mymodulename_registration_user_validate_mail(trim($form_state['values']['email'])) == 1) {
        form_set_error('email', 'Email address already registered');
      } 
        // Validate password.
      if (trim($form_state['values']['password']) == '') {
        form_set_error('password', 'Password field is required');
      }
      if (trim($form_state['values']['password_confirm']) == '') {
        form_set_error('password_confirm', 'Repeat password field is required');
      }
      if (trim($form_state['values']['password_confirm']) != trim($form_state['values']['password'])) {
        form_set_error('password_confirm', 'Passwords do not match');
      }
    }

/**
 * Helper function to validate email adress already exists or not.
 *
 */
function _mymodulename_registration_user_validate_mail($email) {

  // used db_query_range() to check if email exists or not.
  if ((bool) db_query_range("SELECT mail FROM {users} WHERE mail = :name", 0, 1, array(':name' => $email))->fetchField()) {
    return 1;
  } return 2;
}
  • there might be fields in the snippet which may not match the fields you might have, you have to figure out them. For making form field required, you can use '#required' form api property as well. Anyway validation is 3 step process. 1. hook into the form 2. add your custom validation handler 3. add callback for your validation – arpitr Sep 9 '13 at 5:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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