By xeontheone on
I have a simple form, a simple validation function, and a simple submit function.
My question is:
when doing validation, if something isn't as I want it, I want to return the form to the user, displaying the user with a warning message, and supplying him/her with a new value in the field.
A simple thing, but I'm unable to accomplish it.
I've tried with different codes, no luck. Check them out.
Please someone illuminate me!
function form_regform($form_state) {
$form['userGN'] = array(
'#type' => 'textfield',
'#title' => t('a field'),
'#required' => true,
'#default_value' => "default value"
);
return $form;
}
function form_regform_validate($form, &$form_state) {
if (<my condition>)
{
form_set_error('', "Error message );
// *** ALL OF THESE DON'T WORK - THE FIELD IS RETURNED TO THE USER WITH THE VALUE THE USER PUT IN IT WHEN SUBMITTING ***
form_set_value($form['userGN'], "Choose another", $form_state);
$form_state['values']['userGN']['#default_value'] = "Choose another";
$form_state['values']['userGN'] = "Choose another";
$form['userGN']['#value'] = "Choose another";
}
}
Comments
The last one
might work if you accept the first parameter by reference, ie (&$form, &$form_state).
Another completely untested idea would be to use a hidden field and change it in $form_state, and use some jquery to test the hidden field and set the userGN value.
www.purpleoar.co.nz/scryptik - Javascript editor with syntax error checking
www.purpleoar.co.nz - Web development, Drupal consultancy
Thank you for you answer. I
Thank you for you answer. I will try using a reference to the $form. I didn't set it because the APi documentation state that I have to use this declaration:
drupal_validate_form($form_id, $form, &$form_state)
(http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_vali...)
In the meantime, some other suggestion?
I've tried, setting a
I've tried, setting a reference to $form (&$form), no luck.
Some other suggestion? Is
Some other suggestion? Is that possible at all?
I've tested that form_set_value() correctly set the value if I make it continue to the _submit() function, in fact in the _submit() function I get the modified values. I don't get them in the form displayed to the user when I set form_set_error()
Well, I found a way to do it,
Well, I found a way to do it, though it's a completely undocumented behaviour.
I discovered that, to display the form to the user with values of my choice, set during the _validate() phase, I have to rebuild the form and NOT use form_set_error().
To rebuild the form, I use $form_state['rebuild'] = TRUE;
To display to the user a message explaining why the validation phase failed, instead of form_set_error() I simply use drupal_set_message("message", 'error')
Summarizing, in the _validate() function, I have this code:
Fortunately, setting $form_state['rebuild'] to TRUE is sufficient to make Drupal NOT call the naturally following _submit() function. At least this......!
So, solution found. Hope it's useful to someone in the future.
Cheers
xeon
I was having the same
I was having the same dilemma. Thanks so much, this saved a lot of time!
Shabana Navas
snavas@acromedia.com
Software Developer
Acro Media
https://www.acromedia.com
1-800-818-4564
Skype: super.shaba