I have a form with a textarea. When you click submit a function 'mymodule_form_submit()' is called and within that a function that checks the content of the textarea called 'mymodule_check_before_submit()' is called to make sure that the content that the user will try to add follows certain guidelines. If it does not the content of the textarea should remain and an error message is given to the user who is given a chance to correct their mistake.

I have gotten the error message to appear but the textarea gets cleared. How can I fix this so that the content of the textarea stays?

Comments

ank.g9’s picture

by using hook_form_alter() you can initialized a session variable to store textarea value at validation time.
and use this session variable as a default value of textarea.

skipyT’s picture

Hi,

You should do a textarea value validation.

in the form build function:

$form['mytextarea'] = array(
'#type' => 'textarea',
'#element_validate' => 'mymodule_mytextarea_validate',

And you make a function:

function mymodule_mytextarea_validate($element, &$form_state, $form) {
  // You can verify the textarea value here.
   if (empty($element['#value'])) {
     // If not ok send error. The form value will be kept and thee submit function will not be called.
     form_error($element, t('This field is required.'));
   }
}
numfar85’s picture

When I go through the textarea text I create an error array that contain all the possible different problems with the text, when calling form_error() can I iterate over the array? Right now I do this to show the error messages:

    foreach($error as $e) {
      drupal_set_message(t($e),'error');
    }

I tried doing something like this but it didn't work:

  $form['adding_textarea'] = array(
                                     '#type' => 'textarea',
                                     '#element_validate' => '_molmeth_textarea_validate',
                                     '#id' => 'protocol',/* If changed, changes also need to be made in the javascript jqtestfor.js */
                                     '#attached' => array(
                                                          'js' => array(
                                                                        //javascripts the enables tagging with buttons                          
                                                                         drupal_get_path('module','molmeth') . '/jqtestfor.js',
                                                                        ),
                                                          ),
                                   );

and

function _molmeth_textarea_validate($element, &$form_state, $form) {
  $protocol = $form_state['values']['adding_textarea'];
  if ($error = _molmeth_check_before_submit($protocol)) {
    foreach($error as $e) {
      form_error($element, t($e));
    }
  }


}

The function _molmeth_check_before_submit() returns a(possibly empty) error array.

numfar85’s picture

The error message I get say:

"Warning: Invalid argument supplied for foreach() in _form_validate() (line 1378 of /var/www/sity/drupal-7.12/includes/form.inc)."

coleman.sean.c’s picture

'#element_validate' => array('_molmeth_textarea_validate')

#element_validate is an array of function names, so that other modules can use hook_form_alter to add their own validation to the list without over-writing what comes before.

numfar85’s picture

ah, yes, changing this makes it work. But I can't get it to show more then one error message. Can form_error() only be used to give one error message or should it be possible to loop over it giving it a bunch of different error messages or should that be done in a different way?

hemant_gautam’s picture

Hi,

I have gone through your query and thought of giving you a solution. May be it can help you.

For validating the the text area content, you can use hook_validate() function and put the validation whatever you want.

For rendering the content after click submit. you can provide this code before your form element.

$default_text_area_value = isset($form_state['values']['text_area'])?$form_state['values']['text_area']:'';
			 $form['text_area'] = array (
					'#type' => 'textarea',
					'#default_value' => $default_text_area_value,
					'#title' => 'Enter Text',
			);

I hope, it will help you.