Here's my issue. During user register I'm adding fields via form_alter and creating a stripe account. When validation fails $form_state gets wiped out and when the user passes through validation again a second account gets created.

I'm thinking about using cache_set to hang on to the account info until the user gets through the registration process error free but is there a better way to do this?


It sounds like before your mymodule_form_validation($form, &$form_state) function returns you want to store the Stripe information in the users $_SESSION array as a valid attempt/data.

You write to user session using the global session value in PHP and Drupal adds it to the Users' DB/Mongo user session info at the end of page execution. For example:

// in your form validation code ...
$_SESSION['stripe_username'] = 'john_doe';

On form load simply prepopulate the form data from $_SESSION. Eg, you're stuffing the content into session because it will be wiped on form reload -- then you're replacing it so a 2nd account isnt created.

  • I thought about using sessions for this but I don't know how sensitive the customer id is to stripe (really the only thing I want to persist). – Jim Stamper Jun 20 '13 at 22:35
  • Reading up on sessions again and this may be the easiest way to go and it's not nearly as insecure as I thought it might be. – Jim Stamper Jun 20 '13 at 22:51

Another option for this particular use case may be

To set the form value during validation.

Use this function to change the submitted value of a form element in a form validation function, so that the changed value persists in $form_state through the remaining validation and submission handlers.

For example, on validation error, you could store the stripe ID in a '#type' => 'value' form element and thereby make it available for the subsequent request.

  • It won't work. FAPI will only persist things with a #default_value. So hidden elements and the $form_state['values'] array get blown away when the form is rebuilt after failed validation. You can do some trickery with $form_state['rebuild'] = true but I would prefer to keep this as straightforward as possible. The key here is the last part of the sentence - through the remaining validation and submission handlers.. See form_builder. – Jim Stamper Jun 21 '13 at 13:13

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.