Hello..

How to refresh form_token or reset the form when it is failed on validation ?
I want to prevent user from refreshing the same submission.

I check on the code that the same form_token is being use when it is failed or it is success on submission.
Can I refresh the form_token when it is fail and it is success?

I check from example modules, all the forms also same, using same form_token that are hidden.

Comments

Jaypan’s picture

What is your end goal here? What are you trying to prevent, and why?

Dorayaki’s picture

My goal is I don't want user to submit when they are failed. I want the form to rebuild when it is failed on validation.

I'm using drupal with some credit issue.
First the user doesn't have enough credit to process, so the user failed when he submit because the validation is checkin if the user has enough credit. The user top-up and change his mind, but he accidentally press refresh and it is submitted.

I'm trying to prevent this. I tried to use $form_state['rebuild'] after form_set_error, but no luck. :(

Jaypan’s picture

Sorry, I don't understand your issue enough to be able to help.

Dorayaki’s picture

Ok how about

If I have a form then it is failed on validation...
I want the user not able to submit by "Refresh"
When the user refresh the page, user should get new form instead getting the same error.

Example form:

function calc_amount_form($form, &$form_state) {
$form['amount'] = array(
	'#type' => 'textfield',
	'#title' => t('Input amount),
	'#default_value' => '',
	'#size' => 20,
	'#maxlength' => 14,
	'#required' => TRUE,

$form['submit'] = array(
	'#type' => 'submit',
	'#value' => t('Submit'),
);
}
function calc_amount_form_validate($form, &$form_state) {
if(!is_numeric($form_state['values']['amount'])){
	form_set_error('amount',t('Amount should be numeric.'));
}

function calc_amount_form_submit($form, &$form_state) {
drupal_set_message('Calculated');
}

That is an example..
when the user input character, the user will got error from validate function. When user refresh page by pressing ctrl+r or f5, the field of the form is not blank, instead the form will re-submit again and it will shows same error message again.

I mean is there a way to clear all the fields after form_set_error ?
or is there a way when user refresh the page, previous data is not re-submit.

Jaypan’s picture

None that I know of. This is the way browsers handle posted data, it's not a Drupal thing. The question is why is this an issue?