Hi,

I have sucessfully done a form validation on a specific user field, a file field to be exact.
My code runs perfectly and does exactly what is should.

Now, in user profile, i have also defined a field, named field_fileparsedok which is originally a checkbox which can be of values
0 or 1.

The problem that i now really dont understand is when my validation runs on the file my code looks like this
(pseudocode)
...
setFieldValue(0);
if (fileparsedok) setFieldValue(1);

The problem is, it never saves the value in the user profile, exactly as if is overwritten by the original value that was before
my validation. I have tried everything and it really doesnt save to the user profile...
Any ideas what could be the problem?
How do i set a user profile field_ value inside a validation hook working on some other field_ of the user fields?

Comments

nevets’s picture

I believe you are looking for form_set_value(). And please note, real code is more useful than psuedo code for people who provide help.

arne_hortell’s picture

Yes, i understand that pseudo code is not as good as real code but the project is classified and the sourcecode is tremendous between the important parts. Anyway, form_set_value i think is not useful as the field that should be changed is hidden and not included in the form at all.

Nothing is impossible, the impossible just takes a little more time

jaypan’s picture

This is why it's hard to debug without seeing proper code:

the field that should be changed is hidden and not included in the form at all.

I have no idea what this means, but it seems to be a fairly integral piece of the information that was left out of the original post.

Contact me to contract me for D7 -> D10/11 migrations.

arne_hortell’s picture

// *********************************
// $testflag, which is a boolean, TRUE or FALSE
// $user is a drupal-user object which i got from
// $user=user_load_by_name($REAL_USER_NAME)
// $user exists and contains correct data.
// $REAL_USER_NAME is a string and contains a valid username.
// *********************************
$i=0;
if ($testflag==TRUE) $i=1;
$user->original = clone $user;
$user->field_special['und'][0]['value']=$i;
field_attach_update('user', $user);
// ********************************
// Here i want the vaule of
// $user->field_special['und'][0]['value']
// To be set to either 0 or 1 depending on $testflag.
// as simple as that.

Nothing is impossible, the impossible just takes a little more time

arne_hortell’s picture

In a totally other part of the system, which is NOT inside validation but just "normal" execution, this code works perfectly:
// **************************************
$user->field_otherthing['und'][0]['value']=mt_rand(1,9);
$user->original = clone $user;
field_attach_update('user', $user);
// **************************************

Nothing is impossible, the impossible just takes a little more time

arne_hortell’s picture

Hi All,

After hours of debugging i have finally succeded to save the value i wanted after file validation when uploading.
The thing now is IF user presses form save button it overwrites the already written value when file validation was done.
Why?

The actual value is "hidden" among fields and is therefore not available in the form itself...
Anyone?

Nothing is impossible, the impossible just takes a little more time

jaypan’s picture

Are you looking for us to just brainstorm a bunch of possibilities?

Contact me to contract me for D7 -> D10/11 migrations.

arne_hortell’s picture

I just want a straight answer,

IF something (say my validation routine) changes user fields.
Does form save, which is done a while (depending on user presses the submit) change ALSO
the user field value, that IS hidden and NOT in the user_edit form at all.

Do you at all understand the question?

Here is the flow:
1. user uploads a file to a user field which is of type file.
2. validation confirms its a valid file and sets a "file is valid" field in user profile fields.
3. The field is actually set at this time. Checked with admin rights on the admin->people->username->edit
4 User presses form submit for user_edit
5. "file is valid" field is overwritten to value before file upload.

Why?

Nothing is impossible, the impossible just takes a little more time

jaypan’s picture

My guess is that it's something you have done in your code.

Contact me to contract me for D7 -> D10/11 migrations.

arne_hortell’s picture

Here is the code part that successfully changes the value for the user field "special"
The issue is actually NOT that it doesnt set the value because it does, temporarily.
When user uploads the file and the validation code is run and the file is ok it calls this function.
The issue is when user later, further down in form presses submit button, it overwrites
field_something with the value it was BEFORE user uploaded and validated the file.

function setUserSomething($username,$myflag)
{
$user=user_load_by_name($username);
if ($user) {
$i=0;
if ($myflag==TRUE) $i=1;
$edit = array( 'field_something' => array(
'und' => array(
0 => array(
'value' => $i
),
),
),
);
user_save($user, $edit);
/*
$obj = entity_metadata_wrapper('user', $user);
$obj->field_something->set($i);
$obj->save();
*/
}
}

The function above is called from
xxx_file_widget_validate(
which in turn is hooked from
xxx_file_alter
which in turn is hooked from
xxx_form_alter

Its really as simple as that, the problem is as said, when user after validated file upload, later press the form submit button in
user/yy/edit the field_something gets overwritten by previous value that $user->field_something had before file upload.
The really crazy thing is that the hooks works perfectly doing what it should, it works uploading, validating and
i simply dont have any hook_save_user that could f**k something up, therefore i think its something i completely misunderstood how drupal saves user fields in user/yy/edit or there is a tiny flaw in drupal. As the $user->field_something is a hidden field it doesnt even show in the form so it is nothing that should be read when updating user values with the save form functions.
As you see above i have also tested the entity method and it gives the same result.

Nothing is impossible, the impossible just takes a little more time

arne_hortell’s picture

Suppose we start again, how would you implement this

1. An extra, not visible field in user, (field_something, INT 0= default means field_something is not accepted, 1=file ok)
2. An extra, visible file upload field in user field_myfile
3. file validation on upload, if file uploaded, field_myfile is ok field_something=1, if not field_something=0
4. file validation on save,, if file uploaded, field_myfile is ok field_something=1, if not field_something=0

Any ideas?

Nothing is impossible, the impossible just takes a little more time

jaypan’s picture

Can you tell me in plain English what you are trying to accomplish? What is the workflow. Who is the user, and what are they trying to do? You have delved into the fine points, but I don't know the big picture yet.

Contact me to contract me for D7 -> D10/11 migrations.

arne_hortell’s picture

A user have registered itself and entered a few additional required values.
When user log into the system it is required that the user uploads a file of a special kind.
When the file is uploaded it should be validated and if the file is ok a user variable should tell that the uploaded file is ok
(If the file is not ok it should tell user that file is bad and not allow upload/storing it)
This user variable must be stored in the users fields as it is required later and can not be real time validated against the file each time.
Actually not harder than that.

Nothing is impossible, the impossible just takes a little more time

jaypan’s picture

And when is this happening - is it part of the sign up process? Or afterwards? What form are you trying to do this on?

Contact me to contract me for D7 -> D10/11 migrations.

arne_hortell’s picture

In the registration process, but to be able to login this is not required but more of letting the user get more functionality.
I work on the user profile form.

It could have been done in the registration process but it's decided
it should be done as a second level of feature activation instead.

First level, user is registered, user can do easy things.
Second level user can do special things.

You could see it as authenticated user get into some other user role actually, when providing validated file.

The problem as I see it is that user is uploading the file the validation works perfectly and the second level flag is stored as it should.
Now the form isn't really saved as user only uploaded the file so the user presses save in the form.
When this save happens it overwrites the second level flag with value before it was set in the validation, which is "not a second level user"

As I understood things using roles won't work either as user_save is overwriting with old values as mentioned above.

Nothing is impossible, the impossible just takes a little more time

jaypan’s picture

I guess my question is, is this validation function part of a custom form you have built, or are you tying into a form that has been generated by Drupal core or some module?

Contact me to contract me for D7 -> D10/11 migrations.

arne_hortell’s picture

Is tied to user profile form, totally generated by drupal

Nothing is impossible, the impossible just takes a little more time

arne_hortell’s picture

The problem is still the same,
it does not matter if i use any method,
user_save(in any form, entity or user object) does not work inside
a file_upload_validation function
and user_save does not work in form validation or form submit.
The values ARE set but a very short time after the new correct value
is replaced with values before they where set.

I have also tried setting the user in a specific role but as that is dependent on user_save
it doesnt work either.

Is anyone capable to confirm this?

Nothing is impossible, the impossible just takes a little more time

arne_hortell’s picture

Here is what happens

When using file validation the file is uploaded and validated.
This can be processed in a good manner.
Now to "transfer" uploaded file to user field you must press save
to submit the form.
When this happends ONLY values submitted in form
Will be altered and other values will be what they where when form was rendered.
This means regardless of method nothing that's stored in validation will be there after form submit.
You can NOT use user_save anywhere as that is called in form_submit.
The only thing possible is altering form values
Before submit is finished.

Nothing is impossible, the impossible just takes a little more time

arne_hortell’s picture

When you do file validation, the actual file you are validating is a temporary file
and until the validation is finished it have the name of
$file->uri
NOT
$file->filename

Depending on validation time, this can be an issue.
When later coming to user_save the file is actually saved under the "right" name...

Nothing is impossible, the impossible just takes a little more time