In Drupal 6, how can a function that builds a multi-page form see the results of the validate function? - drupal

In Drupal 6.20 I'm building a function that creates a multi-page form (my_form_process). On one page the user can select from a list of previously created addresses, or create a new one. If the user wants to create a new one, I use javascript & CSS to hide the selections and unhide the address form (address, city state zip, etc). To make sure the user provides the required data, I have a validate function (my_form_process_validate) that I set the error if the fields are blank. The trouble is that when the pages fails the validation, the my_form_process redraws the form with the validation errors, but defaults back to the "select from a list of previously created addresses", and the "create new" form elements with the errors is still hidden.
How can I tell the my_form_process that builds the form, when the validation has failed show the "create new" div, and hide the "select old" div? I've tried setting a $form['storage'] variable but the my_form_process doesn't seem to see it.
Here is some more info:
I tried adding a $form_state['validation_status'] = 'error' in the validation routine but still found that value not in the $form_state array used in the form function when an error was found.
However, when there was no error, the value was available. (a clue)
So, just for kicks, I removed the "form_set_error" line from the validation routine and now the value is available.
So now, I have to set
$form_state['validation_error'] = '<ol>';
$form_state['validation_status'] = 'error';
$form_state['validation_error'] .= '<li>' . t('address cannot be blank') . '</li>';
$form_state['validation_error'] .= '</ol>';
Then in the form function I can test the $form_state['validation_error'] and if 'error' then use
drupal_set_message($form_state['validation_error'],$form_state['validation_status']);
To display the message and then set the display property of the div's appropriately.
Sure seems like a bug in the "form_set_error" process, but I'm a newbie in Drupal/PHP land so I'm not really sure what I'm doing.

You should be able to pass values through the $form_state variable back and forth between the form function, validation function, and submit handler functions. This might help (if you haven't looked at this already) http://drupal.org/node/144132#multistep

Related

Run checks on drupal_goto function

I have a strange issue with a drupal site and a custom form. I have a custom module that puts a form as a block...simple input field that posts a search query to a store locator.
The form keeps getting overridden and the action/url changes randomly at random times. I need a quick solution to this whilst we debug the real issue that's causing this.
I need to write an if statement that checks the drupal_goto function that goes along the lines of if drupal_goto == /store-locator then run else change the drupal_goto action to /store-locator.
Any help would be appreciated.

Outputing Drupal Webform Values to Highrise

I am integrating Drupal Webform with the CRM Highrise - I'm using the Highrise Drupal module (https://drupal.org/project/highrise) to create some mapping - however I want to extend the module to pass additional form values to the "background" field designated within Highrise, the issue I'm running into is that rather than passing the form value of say "Birthday Party" it's simply returning "array" in Highrise, below is the code I have right now:
//initial variable declaration
$form['#get_eventtype'] = drupal_render($event_type);
//making the call to post to Highrise
case 4:
$background = $form_state['values']['submitted'][$row['cid']];
$background .= $form['#get_eventtype'];
$new_person->setBackground($background);
break;
Have you dumped the value in $background after you retrieve it from the form? It is going to be an array and your value will be in one of the array elements. If you haven't already, I would suggest installing the devel module to help you dig into the form structure.
As a FYI I ended up just using Formstack.com - which gives you the ability to easily map custom fields created in Highrise with the form you create using Formstack. Upon creating a form with Formstack I embedded the JavaScript tag provided and was good to go.

Show a previous page value in Drupal multi-step webform?

In a multi-step Drupal webform, is it possible to show a value entered in a previous-step page?
For example, the first step page captures a username field as "John", is it possible to show a greeting in next step page, showing Hello John, ... ?
p.s. for anonymous user.
I know this is old, but I needed the same thing so I hope this helps others in search.
I needed values from a previous Webform step for my custom Webform component. The _webform_redner_[component](...) hook doesn't provide the form or form_state, also the previous steps' data (more than previous step that was just submitted) isn't in the $_POST.
To solve this, I manually retrieve the form from Drupal's form caching system using the form_build_id which is in the $_POST variable.
/* ... */
$form_state = array();
// Get the form_state to pass on to our build function.
// Webforms doesn't provide it at this point so we'll need to manually get it using the form's build_id.
$form = form_get_cache($_POST['form_build_id'], $form_state);
/* ... */
You now have the entire form and form_state that also includes previous steps' values.
Have a look at multistep form example here: http://drupal.org/node/717750
The general idea is that in the submit function you save all posted values to $form_state['storage'], which you can later access on the next steps.
Edit:
Have a look at the example with both Prev and Next buttons I have just created: http://zgadzaj.com/basic-drupal-multipart-form-example-with-previous-and-next-buttons

Drupal using views with CCK custom fields

I've got a Drupal site which uses a custom field for a certain type of node (person_id) which corresponds to a particular user. I want to create a view so that when logged in, a user can see a list of nodes 'tagged' with their person_id. I've got the view working fine, with a url of my-library/username but replacing username with a different username shows a list of all nodes tagged with that user. What I want to do is stop users changing the URL and seeing other users' tagged nodes. How can I do this? Is there somewhere where I can dictate that the only valid argument for this page is the one that corresponds with the current logged in user's username?
person_id = uid?
In this case, add argument with user:uid, then in Validation options select PHP Code, read comment of this field carefully:
Enter PHP code that returns TRUE or
FALSE. No return is the same as FALSE,
so be SURE to return something if you
do not want to declare the argument
invalid. Do not use . The
argument to validate will be
"$argument" and the view will be
"$view". You may change the argument
by setting "$handler->argument".
Add this code:
global $user;
$account = user_load('name'=>arg(1));
$handler->argument = $user->uid;
return $account->uid == $user->uid;
I'm not sure how you have setup your view, which gives some different options to solve this. A way that should work would be to set the default argument be the logged users id/username and remove the argument from the url.
Alternatively you could create your own filter which requires some work with the views API, but gives more control.

change user_profile_form form fields order

When a user login , the user will be redirect to a user profile page, which has a My account field set.
the field set has 2 fields, "Username: ", "Email address:". those 2 fields are generated by drupal.
those 2 field contained in a form which has a id ("user_profile_form") . I want to change the order of those 2 fields.
I have tried to intercept 'user_profile_form' , inside hook_form_alter.
code as follow:
$form['account']['name']['#weight'] = 1;
but that did not success, drupal did not even rendering the 'name' field, so no username: showed on browser.
What you did is absolutely correct, and probably did work. You can change the weight of the fields with the method described above.
The username field is not always rendered. The reason is that a persmission is required: change own username. If that perm is not set, you wont be allowed to alter you username and the field wont be shown.
Info on debugging.
Your info alone is not quite enough to debug. From what you describe, you are doing the right thing, but other modules could be making things a bit tricky for you. The devel module is quite good when it comes to debugging, ti defines two functions I use a lot when debugging:
dpm() pretty prints the variable to the message area using krumo.
dd() Prints / saves a variable to a log file. Useful when you can't view messages on the screen.
I would suggest that you look at the $form variable before and after you alter it.
Things that could make it go wrong:
Did you remember to pass the $form variable by reference using the & notation?
Is another module altering your form after you?
Are you checking for the correct form id, so you alter the correct form?
These are some pointers, before you bring more info, all I can do is guess to what your problem exactly can be. I did something like this a few days ago so I know what you describe shouldn't be a problem.

Resources