form_set_value is kicking my butt.
I am modifying the Signup module for my own purposes -- my users have ID cards with barcodes on them, and I want them to scan in their card in order to sign up for a module. The plan is that they'll scan in their card, and the form will look up their card # and connect it to a username. Then, so as to not reinvent the wheel, I'll pass in username to existing functions in Signup and they'll take it from there.
Here's my issue: I have this validate function where I'm barcode-matching, and I can not for the life of me figure out how to insert the username back into the form values. The existing Signup functions look for $form_state['values']['signup_username'], so I want to insert the name in there so those functions will be happy and process it.
function signup_form_validate_barcode($form, $form_state) {
$barcode = $form_state['values']['signup_barcode'];
// a bunch of stuff happens here that matches the barcode and gives me a $username
form_set_value($form['values']['signup_username'], $username, $form_state);
}
I have read every bit of documentation on form_set_value and I just can't wrap my head around how I'm supposed to format the first argument. Right now I get an array_shift() error, but, but... HOW is it supposed to be an array?? (Also, does the signup_username field need to exist, hidden on the form, in order for me to sneak values into it during validation?)
Thanks for any help!
Eileen
Comments
As an example:
As an example:
The array in question holds a "map" to the value to be changed. If the value to be changed, with respect to the form array, is at $field_name[0]['value'], then the array $x in the example above will tell form_set_value what it needs to know. You can call the array whatever, but form_set_value looks in $whatever['#parents'] for the array that actually maps to the value to be changed. So you can't just use
$whatever = array("$field_name", 0, 'value');
, you have to set$whatever['#parents'] = array("$field_name", 0, 'value');
and then pass in $whatever.Or, in your example, it's probably
$x['#parents'] = array('signup_username');
I hope that's clear enough. It certainly took me long enough to understand it myself, and then they changed the rules in D6 ;-)
Ohhhhh, OK. That makes
Ohhhhh, OK. That makes sense. (Kind of!) Thanks for giving a simple example -- all the documentation addresses multi-dimensional arrays and it was driving me crazy to just try to set my one single value.
Soooo.... follow-up question: My validation form looks at the barcode, gets the username from the DB, and sets the signup_username in $form_state. But the submit function doesn't get the new value that was set in $form_state; it looks like that function is sent the original, un-altered $form_state. Do I need to do something special to have the new+improved $form_state from my validate function shoved into the submit function?
Thanks!
I don't follow...
If you change a form value with form_set_value using hook_validate, the changed value is what gets saved, provided you passed in $form_state by reference, i.e.,
function hook_validate($form, &$form_state)
(as opposed tofunction hook_validate($form, $form_state)
).Perfect!! I had no idea
Perfect!! I had no idea about passing in the reference with &. Thanks for all the help!