I am validating a form containing text in a textarea field. I have found that when my validation function is called the data in the form's textarea field is not in $form_state (or whatever the second argument's name in the validation function definition is) as expected. The form's textarea key is in $form_state as a subkey of a values key, but the the value associated with that subkey is a zero length string. Instead I find the expected text is in the $_POST variable and its key is named after the submitted form's textarea's key.
From all I have read the text is supposed to be in $form_state. All the examples I have seen use the $form_state variable to get this data instead of from $_POST. I would like my code to get if from $form_state as this appears to be the proper way to do it. What could cause the text in the textarea field not to appear in there?
Comments
Absolutely the value in
Absolutely the value in validation callbacks should be in
$form_state['values']. It's impossible to tell what the problem could be without seeing your form and validation callbacks. Generally, a validation callback should look something like:Make sure the function signature is correct. Form values can always be found in
$form_state['values']. If you want more insight please post your complete form and validate functions so we can check it out.I have just noticed
I have just noticed at:
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
there are two keys that seem to do the same thing. These are #validate and #element_validate. What is different between them?
I am using #validate in the textarea field's array because this textarea field has its own submit button and when it is clicked on I want only to validate this field. I will try #element_validate and if this does not make a difference I will post code.
The difference between the
The difference between the two keys is that they are intended for different items. Generally #validate should go in $form['#validate'] and will validate every item in the form.
#element_validateis meant to be part of a specific form element and will server to validate that element upon submit. it's usually used to do things like validate that a specific form value is an integer with a commonly used integer validation function. If you want to have separate submits on your form then you may need to use separate form functions all together. Then, in your form builder concatenated them togetherThen, on each form use
$form['#validate'] = array('mymodule_validate_callback');. That way you can separate the validation of the form that contains the textarea and submit from whatever other form exists on the same page. I could get more specific with the code your actually working with if you post it, but that's the general idea.Using #element_validate
Using #element_validate instead of #validate did not make a difference. So here is the code. The trouble is in function _db_prefixes_add_validate() of file ncludes/db_prefixes.table_names.inc (second file below).
File db_prefixes.module:
File includes/db_prefixes.table_names.inc
File includes/db_prefixes.table_names.inc
This problem is solved. It
This problem is solved. It went away when I changed keys from 'Add Prefixes' to 'Add_Prefixes' and ''Add Prefixes Text' to 'Add_Prefixes_Text'. Is it documented anywhere that keys in forms cannot have spaces? If not it should be.
That's a good question! I'll
That's a good question! I'll check it out and certainly can update documentation if not.