0

I'm creating a simple contact form and it works flawlessly, besides the validation.

When I try to exercise the validate function, no error messages are shown. The text in the correspondent field turns red, so that works, but where is the messages?

If I fill in the form correctly and the mail gets sent, messages are shown just like they should.

Here's my code:

function mymodule_buysell_contact_form($form, &$form_state) {
    // The form

    $form['contact'] = array(

        '#title' => t('Skicka ett meddelande till säljaren'),

        '#type' => 'fieldset'

    );
    $form['contact']['message'] = array(

        '#type' => 'textarea',

        '#title' => t('Meddelande:'),

        '#cols' => 35,

        '#rows' => 6,

        '#resizable' => FALSE

    );
    $form['contact']['name'] = array(

        '#type' => 'textfield',

        '#title' => t('Ditt Namn:'),

        '#size' => 35,

        '#maxlength' => 30

    );
    $form['contact']['compname'] = array(

        '#type' => 'textfield',

        '#title' => t('Företag:'),

        '#size' => 35,

        '#maxlength' => 30

    );
    $form['contact']['phone'] = array(

        '#type' => 'textfield',

        '#title' => t('Telefon:'),

        '#size' => 35,

        '#maxlength' => 30

    );
    $form['contact']['cellphone'] = array(

        '#type' => 'textfield',

        '#title' => t('Mobiltelefon:'),

        '#size' => 35,

        '#maxlength' => 30

    );

    $form['contact']['mail'] = array(

        '#type' => 'textfield',

        '#title' => t('Din E-mail:'),

        '#size' => 35,

        '#maxlength' => 30

    );

    $form['contact']['captcha'] = array (
        '#type' => 'captcha',
        '#captcha_type' => 'image_captcha/Image',
    );

    $form['contact']['submit'] = array(

        '#type' => 'submit',

        '#value' => t('Skicka')

    );

    return $form;
    }

    function mymodule_buysell_contact_form_validate($form, &$form_state) {
    // Validate message

    $valid_message = $form_state['values']['message'];

        if (empty($valid_message)) {

        form_set_error('message', 'Du har inte skrivit något meddelande');

        }
    // Validate if a name is submitted

    $valid_name = $form_state['values']['name'];

        if (empty($valid_name)) {

        form_set_error('name', 'Du har inte fyllt i ett namn');

        }

    // Validate if a company name is submitted

    $valid_compname = $form_state['values']['compname'];

        if (empty($valid_compname)) {

        form_set_error('compname', 'Du har inte fyllt i ditt företagsnamn');

        }
    // Validate phone by look for numbers

    $valid_phone = $form_state['values']['phone'];

        if (!is_numeric($valid_phone)) {

        form_set_error('phone', 'Ditt nummer verkar inte vara korrekt ifyllt (endast siffror)');

        }
    // Validate cellphone by look for numbers

    $valid_cellphone = $form_state['values']['cellphone'];

        if (!is_numeric($valid_cellphone)) {

        form_set_error('cellphone', 'Ditt mobilnummer verkar inte vara korrekt ifyllt (endast siffror)');

        }

    // Validate the mail, using Drupals own function

    $valid_email = $form_state['values']['mail'];

        if (!valid_email_address($valid_email)) {

        form_set_error('mail', 'Din Epost verkar inte vara korrekt ifylld');

        }
}
  • Did you try disabling the CAPTCHA module, and removing the CAPTCHA field in the form? – kiamlaluno Aug 14 '11 at 21:35
  • It is not necessary to use a validation handler just to verify if a form field is empty; it's enough you use the property "#required" and set it to TRUE. – kiamlaluno Aug 15 '11 at 1:17
  • I did try to remove the CPATCHA, but it didnt work.. And thank you for the tip, I'll try that. – Daniel Holm Aug 18 '11 at 11:07
  • By the way, t() is only used for translating from English to the language set for the Drupal site; the string passed to t() needs then to be in English. If you want to only to show the user interface's strings in a specific language, then you should not call t(). – kiamlaluno Aug 18 '11 at 11:23
  • Thank you. I have removed that now. The #required = TRUE isnt showing any error either... – Daniel Holm Aug 18 '11 at 12:34
1

For fieldsets I think you have to include the parent as well.

Try changing

form_set_error('phone', 'Ditt nummer v.... to

form_set_error('contact][phone', 'Ditt nummer v....

You can read more here: http://drupal.org/node/678816

  • 2
    Sorry, that did not help. Using D7, btw and this exact thing works on another form in the very same module. – Daniel Holm Aug 14 '11 at 20:12
  • As far as I can see, Drupal 7 uses the name of the field only, too; the syntax you are referring is used when the property `#parents" is set for the form field for which you are reporting the error. – kiamlaluno Aug 18 '11 at 11:19
  • Right? But it just wont work.. – Daniel Holm Aug 18 '11 at 12:50

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.