Suppose, I want to use Drupal libraries inside a separate php file to generate a form. I created 3 functions:

  1. function add_form
  2. function add_form_validate
  3. function add_form_submit

And then used drupal_get_form / render to get the HTML code. But when I insert invalid data, I don't see the error message - just the red border of the text input. Here is the minimal example to reproduce the error (file is called test.php and is located inside Drupal directory):

//including Drupal libraries
require_once(DRUPAL_ROOT . DIRECTORY_SEPARATOR . 'includes'.  DIRECTORY_SEPARATOR . 'bootstrap.inc');

//form with 1 text field and submit button
function add_form($form, &$form_state)
    $form['text'] = array(
        '#type' => 'textfield',
        '#title' => 'Insert text',
        '#required' => true,
        '#attributes' => array('autofocus' => 'autofocus'),

    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',

    return $form;

//text must be 'aaa'
function add_form_validate($form, &$form_state)
    if ($form_state['input']['text'] != 'aaa')
        form_set_error('text', t('Text is not aaa!'));

function add_form_submit($form, &$form_state)
    print "I am inside add_form_submit<br>";
<!DOCTYPE html>
    <link type="text/css" rel="stylesheet" media="all" href="/modules/system/system.base.css">
    <link type="text/css" rel="stylesheet" media="all" href="/modules/system/system.theme.css">
    <link type="text/css" rel="stylesheet" media="all" href="/modules/system/system.messages.css">
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
//here is the problem: I don't know, how to output the error message
print theme('status_messages');

$content = drupal_get_form('add_form');
$content = render($content);
print $content;

It is working fine for me, only thing is that I get the error message only when submitting for the second time, that is because you are printing the status message before the drupal_get_form(). So if I print the message after drupal_get_form() then it is working as expected.

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.