If I activate the "Require Picture on Registration Form?", I am unable to change any setting on the user profile page and I get "You must select a picture.", even if the picture was uploaded before or is the setting I want to change.

Other info: I can see that in the pictures folder, the picture is uploaded and renamed properly, but somehow the module can't see it correctly.

Please let me know if there is other information I can can help you with.

Thank you for this useful module.

Comments

aleada’s picture

Hi , I had the same problem after enabling the required flag.
I made a hack inside the validation code of the module. It works nice now but it will be very useful if the author fix this issue with his code style. The changes I made are bellow. Please give me your feedback.

function reg_with_pic_user($op, &$edit, &$user, $category=null) {
if (variable_get('user_pictures', 0)) {
switch ($op) {
case 'register':

// setup picture upload in registration form
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
$form['picture']['picture_upload_register'] = array('#type' => 'file', '#title' => t('Upload picture'), '#description' => t('
Your virtual face or picture. Maximum di
mensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_
size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
$form['picture']['pic_selected'] = array(
'#type' => 'hidden',
'#default_value' => 0
);
//
$form['picture']['picture_upload_register']['#attributes']['onchange'] = "$('#edit-pic-selected').val(1);";

// handle required picture
$required = variable_get('reg_with_pic_required', 0);
if ($required == 1) {
// make the field display as if a value is required
$form['picture']['picture_upload_register']['#required'] = TRUE;
// trick the form into thinking there is a normal value
$form['picture']['picture_upload_register']['#default_value'] = 0;
}

return $form;
break;

case 'validate':
// handle a required field if it was set in the admin UI
$required = variable_get('reg_with_pic_required', 0);
$errors = form_get_errors();

// only attempt to validate photo if one is required, or if there are no other errors
if ($required == 1 || count($errors) == 0) {

// validate uploaded picture, taken from user module
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
$file = file_save_upload('picture_upload_register', $validators);

//ORIGINAL CODE STARTS HERE
// if($required && !$file){
//form_set_error('picture_upload_register',t('You must select a picture.'));
//}

//CHANGED CODE STARTS HERE
if ($required==1 && $file=='') {
+ if($user->picture=='' && $file==''){ //check if the user has already set a picture
form_set_error('picture_upload_register',t('You must select a picture.'));
}
} elseif ($required && $file && count($errors) > 0) {
form_set_error('picture_upload_register', t('You must reselect your picture.'));
}

$edit['picture_uploaded'] = $file ? TRUE : FALSE;

} else {
// a friendly reminder to reselect your picture since the browser will clear it
if ($edit['pic_selected'] == 1) {
form_set_error('picture_upload_register', t('You must reselect your picture.'));
}
}

break;

case 'insert':

if ($edit['picture_uploaded']) {
// file repopulates from uploadcache
$file = file_save_upload('picture_upload_register');
$info = image_get_info($file->filepath);
// save picture to correct path and update the row in the user table
$destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid .'.'. $info['extension'];
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $file->filepath, $user->uid);
}
}
break;
}
}

By this way the validation code is checking if the user has already post a picture.
If the user is new the $user->picture is '' and the validation return failed if the user has not submited a picture using the upload form.
Else if the user has already a picture and is making any other change the nested if inside the validation does not return true, so the user can edit his details without the validation error message 'You must reselect your picture.'

Please confirm that this works for u
Thanks!

aleada’s picture

>>>without the validation error message 'You must reselect your picture.'

sorry the failure message is

'You must select your picture.'

<<<<<<<<<<<<<<<<<<<<<<<<<

samiu1287’s picture

Status: Active » Fixed

Thanks for your work and quick response, amatech.

The code had a "+" sign included and needed another } at the end to close the function.
Except for that, the code seems to run properly.

I hope the author will include it in the next release, as this is a big problem, basically making the "required" utility totally nonfunctional in production sites.

Here's the "repaired" code. Thanks again amatech. This works great.

function reg_with_pic_user($op, &$edit, &$user, $category=null) {
if (variable_get('user_pictures', 0)) {
switch ($op) {
case 'register':

// setup picture upload in registration form
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
$form['picture']['picture_upload_register'] = array('#type' => 'file', '#title' => t('Upload picture'), '#description' => t('
Your virtual face or picture. Maximum di
mensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_
size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
$form['picture']['pic_selected'] = array(
'#type' => 'hidden',
'#default_value' => 0
);
//
$form['picture']['picture_upload_register']['#attributes']['onchange'] = "$('#edit-pic-selected').val(1);";

// handle required picture
$required = variable_get('reg_with_pic_required', 0);
if ($required == 1) {
// make the field display as if a value is required
$form['picture']['picture_upload_register']['#required'] = TRUE;
// trick the form into thinking there is a normal value
$form['picture']['picture_upload_register']['#default_value'] = 0;
}

return $form;
break;

case 'validate':
// handle a required field if it was set in the admin UI
$required = variable_get('reg_with_pic_required', 0);
$errors = form_get_errors();

// only attempt to validate photo if one is required, or if there are no other errors
if ($required == 1 || count($errors) == 0) {

// validate uploaded picture, taken from user module
$validators = array(
'file_validate_is_image' => array(),
'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
);
$file = file_save_upload('picture_upload_register', $validators);

//ORIGINAL CODE STARTS HERE
// if($required && !$file){
//form_set_error('picture_upload_register',t('You must select a picture.'));
//}

//CHANGED CODE STARTS HERE
if ($required==1 && $file=='') {
if($user->picture=='' && $file==''){ //check if the user has already set a picture
form_set_error('picture_upload_register',t('You must select a picture.'));
}
} elseif ($required && $file && count($errors) > 0) {
form_set_error('picture_upload_register', t('You must reselect your picture.'));
}

$edit['picture_uploaded'] = $file ? TRUE : FALSE;

} else {
// a friendly reminder to reselect your picture since the browser will clear it
if ($edit['pic_selected'] == 1) {
form_set_error('picture_upload_register', t('You must reselect your picture.'));
}
}

break;

case 'insert':

if ($edit['picture_uploaded']) {
// file repopulates from uploadcache
$file = file_save_upload('picture_upload_register');
$info = image_get_info($file->filepath);
// save picture to correct path and update the row in the user table
$destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid .'.'. $info['extension'];
if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $file->filepath, $user->uid);
}
}
break;
}
}
}

aleada’s picture

Thanks samiu1287 for your response!

I don't have subversion so I can't make a real patch for the code.

The + sign on my code is just to notify that this is an addition to the original code.
Sorry for my misleading code, I am happy that you got the fix concept of my post.
I don't know how to contact and notify the module author for the bug and the fix.
I hope someone who knows how the community works, to inform the author and give a final solution in the next module release.

aleada’s picture

Status: Fixed » Needs review

Hi again,
I noticed that the fix is not working on the /user/ {uid }/edit page....
The loop message "You must select a picture." remains even when the user is select another picture and want to save his changes....
I have a new fix for that. Please review the functionality inside the user edit page and confirm that the issue exists except the the registration process.

aleada’s picture

/**
 * Implementation of hook_user()
 */
function reg_with_pic_user($op, &$edit, &$user, $category=null) {
  if (variable_get('user_pictures', 0)) {
    switch ($op) {
      case 'register':
        print "register";
        // setup picture upload in registration form
        $form['#attributes']['enctype'] = 'multipart/form-data';
        $form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
        $form['picture']['picture_upload_register'] = array('#type' => 'file', '#title' => t('Upload picture'), '#description' => t('<br>Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
        $form['picture']['pic_selected'] = array(
          '#type' => 'hidden',
          '#default_value' => 0
        );
        //
        $form['picture']['picture_upload_register']['#attributes']['onchange'] = "$('#edit-pic-selected').val(1);";

        // handle required picture
        $required = variable_get('reg_with_pic_required', 0);
        if ($required == 1) {
          // make the field display as if a value is required
          $form['picture']['picture_upload_register']['#required'] = TRUE;
          // trick the form into thinking there is a normal value
          $form['picture']['picture_upload_register']['#default_value'] = 0;
        }

        return $form;
        break;

      case 'validate':
        // handle a required field if it was set in the admin UI
        $required = variable_get('reg_with_pic_required', 0);
        $errors = form_get_errors();

        // only attempt to validate photo if one is required, or if there are no other errors
        if ($required == 1 || count($errors) == 0) {

          // validate uploaded picture, taken from user module
          $validators = array(
            'file_validate_is_image' => array(),
            'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
            'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
          );
          $casereg = file_save_upload('picture_upload_register', $validators); // THIS TAKES VALUE ON REGISTRATION 
          $caseupd = file_save_upload('picture_upload',$validators);  //THIS TAKES VALUE ON USER EDIT
          
          $casedel = $_POST['picture_delete'];                // CREATE AN ERROR ON FORM IF USER TRY TO REMOVE HIS AVATAR 
          if($casedel == 1){
           form_set_error('picture_upload_register', t('Picture is mandatory.'));
          }           
    
          if($casereg==''){
           	$file = $caseupd;
           }else{
                $file = $casereg;   
           }

//OLD CODE
//          if ($required && !$file) {
//            form_set_error('picture_upload_register', t('You must select a picture.'));
//          } elseif ($required && $file && count($errors) > 0) {
//            form_set_error('picture_upload_register', t('You must reselect your picture.'));
//          }

//NEW CODE
	if ($required==1 && $file=='') {
		if($user->picture=='' && $file==''){ //check if the user has already set a picture
		 form_set_error('picture_upload_register',t('You must select a picture.'));
		}
	} elseif ($required && $file && count($errors) > 0) {
		form_set_error('picture_upload_register', t('You must reselect your picture.'));
	}



         $edit['picture_uploaded'] = $file ? TRUE : FALSE;

        } else {
          // a friendly reminder to reselect your picture since the browser will clear it
          if ($edit['pic_selected'] == 1) {
            form_set_error('picture_upload_register', t('You must reselect your picture.'));
          }
        }

        break;

      case 'insert':
            
        if ($edit['picture_uploaded']) {
          // file repopulates from uploadcache
          $file = file_save_upload('picture_upload_register');
          $info = image_get_info($file->filepath);
          // save picture to correct path and update the row in the user table
          $destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid .'.'. $info['extension'];
          if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
            db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $file->filepath, $user->uid);
          }
        }
        break;
     }
  }
}

greenbeans’s picture

I'm encountering the same problem...

Re #6, won't this code throw an error if the user tries to delete their picture and replace it with a new one?


          $casedel = $_POST['picture_delete'];                // CREATE AN ERROR ON FORM IF USER TRY TO REMOVE HIS AVATAR
          if($casedel == 1){
           form_set_error('picture_upload_register', t('Picture is mandatory.'));
          }           

EDIT: This version is working for me.


function reg_with_pic_form_alter(&$form, &$form_state, $form_id) {
	if($form_id == 'user_profile_form') {
		// if pic required, don't even give option to delete
		$required = variable_get('reg_with_pic_required', 0);
		if($required) {
			unset($form['picture']['picture_delete']); 
		}
	}
}


/**
 * Implementation of hook_user()
 */
function reg_with_pic_user($op, &$edit, &$user, $category=null) {
  if (variable_get('user_pictures', 0)) {
    switch ($op) {
      case 'register':

        // setup picture upload in registration form
        $form['#attributes']['enctype'] = 'multipart/form-data';
        $form['picture'] = array('#type' => 'fieldset', '#title' => t('Picture'), '#weight' => 1);
        $form['picture']['picture_upload_register'] = array('#type' => 'file', '#title' => t('Upload picture'), '#description' => t('<br>Your virtual face or picture. Maximum dimensions are %dimensions and the maximum size is %size kB.', array('%dimensions' => variable_get('user_picture_dimensions', '85x85'), '%size' => variable_get('user_picture_file_size', '30'))) .' '. variable_get('user_picture_guidelines', ''));
        $form['picture']['pic_selected'] = array(
          '#type' => 'hidden',
          '#default_value' => 0
        );
        //
        $form['picture']['picture_upload_register']['#attributes']['onchange'] = "$('#edit-pic-selected').val(1);";

        // handle required picture
        $required = variable_get('reg_with_pic_required', 0);
        if ($required == 1) {
          // make the field display as if a value is required
          $form['picture']['picture_upload_register']['#required'] = TRUE;
          // trick the form into thinking there is a normal value
          $form['picture']['picture_upload_register']['#default_value'] = 0;
        }

        return $form;
        break;

      case 'validate':
        // handle a required field if it was set in the admin UI
        $required = variable_get('reg_with_pic_required', 0);
        $errors = form_get_errors();

        // only attempt to validate photo if one is required, or if there are no other errors
        if ($required == 1 || count($errors) == 0) {

          // validate uploaded picture, taken from user module
          $validators = array(
            'file_validate_is_image' => array(),
            'file_validate_image_resolution' => array(variable_get('user_picture_dimensions', '85x85')),
            'file_validate_size' => array(variable_get('user_picture_file_size', '30') * 1024),
          );
          
          if(isset($edit['picture_upload'])) {
          	$form_type = 'edit';
          	$pic_field = 'picture_upload';
          }
          else {
          	$form_type = 'register';
          	$pic_field = 'picture_upload_register';
          }
            
					$file = file_save_upload($pic_field, $validators);

					if ($required && !$file && ($form_type == 'register' || !$user->picture)) {
						form_set_error($pic_field, t('You must select a picture.'));
					} elseif ($required && $file && count($errors) > 0) {
						form_set_error($pic_field, t('You must reselect your picture.'));
					}
					
          $edit['picture_uploaded'] = $file ? TRUE : FALSE;

        } else {
          // a friendly reminder to reselect your picture since the browser will clear it
          if ($edit['pic_selected'] == 1) {
            form_set_error('picture_upload_register', t('You must reselect your picture.'));
          }
        }

        break;

      case 'insert':

        if ($edit['picture_uploaded']) {
          // file repopulates from uploadcache
          $file = file_save_upload('picture_upload_register');
          $info = image_get_info($file->filepath);
          // save picture to correct path and update the row in the user table
          $destination = variable_get('user_picture_path', 'pictures') .'/picture-'. $user->uid .'.'. $info['extension'];
          if (file_copy($file, $destination, FILE_EXISTS_REPLACE)) {
            db_query("UPDATE {users} SET picture='%s' WHERE uid=%d", $file->filepath, $user->uid);
          }
        }
        break;
    }
  }
}
aleada’s picture

The code #8 i am not sure but I believe that gives the user the abillity to delete his picture, using the /user/##/edit page without to force him upload a new one.
I believe that the picture must be required both on user registration and user edit page. Am I wrong?

greenbeans’s picture

Yes, #8 disables the ability to delete the picture. My testing revealed that if you check the delete box and upload a new one at the same time, it just gets deleted and the new one isn't saved. If the pic is required, that shouldn't be permitted. And since it's confusing to the user to have the delete checkbox there if he's not permitted to use it, I just ditched the checkbox.

If you just upload a new one, it's saved as the new avatar and the old one is disassociated with the account (dunno if it's actually deleted).

The other change I made in my version was to make sure that if no new pic is uploaded but there's already a pic attached to the account, no error is thrown.

It may be that the two patches accomplish the same thing but I was having a lot of trouble understanding #6 until I wrote my on version. ;)

aleada’s picture

Well greenbeans,
I am not sure what to do, the #6 throws an error when someone tries to delete his picture . It is not wrong because the picture is mandatory. But it is little confusing for the user the delete picture checkbox.....

I believe that when the picture is required , the user must select always a picture .
This should be applied both on register and edit page. So the best solution is to remove the check box "delete picture", from the edit page.
Does anybody know how to do that?

greenbeans’s picture

The added reg_with_pic_form_alter() function in #8 hides the delete checkbox.

aleada’s picture

Thanks greenbeans!!
Nice work! I hope the module author, use your code in the next version release!!!

mmilano’s picture

Version: 6.x-1.4 » 6.x-1.x-dev
Assigned: Unassigned » mmilano
Status: Needs review » Fixed

Nice work and thanks. I committed the changes and they should be available the next time 6.x-1-dev builds.

aleada’s picture

;) nice work guys!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.